Friday, 9 August 2013

http 415 code when trying to sent a json object

http 415 code when trying to sent a json object

This is my code:
JSONObject json = null;
try{
json =
journalService.takeFromQueue(context.getBundle().getSymbolicName());
}catch(JournalService.NoQueueAcquiredException e){
System.out.println("ERROR in DataBaseModuleImpl.startLogging():
NoQueueAcquiredException");
}
try {
URL url = null;
url = new URL(databaseUrl + "journal");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" +
json.toString().length());
connection.setUseCaches(false);
connection.setDoInput(true);
System.out.println(json.toString());
//send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(json.toString());
wr.flush();
wr.close();
//get response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader (is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null){
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
I've found that 415 means that my Content-Type is not correct (or more
correctly: unsupported media type). But I tried to send a json to my
server, so I don't understand my error...
My json body looks like this:
{
"message":"Logger works",
"level":4,
"argsMap":{ },
"calledFunctionName":"trol",
"BundleNameOfLoggingBundle":"occs.test.service.a",
"uri_id":"uri of id van het datapacket",
"Timestamp":"2013-08-09 14:57:10.21",
"Journalling":true,
"BundleNameOfServiceProvider":"occs.test.impl"
}

No comments:

Post a Comment