CommentTimeJul 23rd 2010 edited @ 07/23/2010 7:13 am
Hi all, I'm trying to make a simple jquery script that send data to a java servlet, that will send request to myintervals, retrieve the response and send this one to the jquery script. So, the problem is the myinterval's response:
<?xml version="1.0" encoding="UTF-8"?>
<intervals personid="59348" status="Bad Request" code="400"><error><code>17</code><message>Parameter not recognized</message><verbose><item>Unable to parse PUT/POST input. Please check your XML/JSON for validity and well-formedness.</item></verbose></error></intervals>
my = (HttpURLConnection)myint.openConnection(); my.setRequestMethod("POST"); my.addRequestProperty("Host", "api.myintervals.com"); my.addRequestProperty("Accept", "application/xml"); my.addRequestProperty("Content-type", "application/json"); my.addRequestProperty("Authorization", "Basic M2k1NnhzY3EzOG86WA=="); InputStream is = my.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { resp.getWriter().println(inputLine); } //resp.getWriter().println(myint); in.close();
}
With this i'm trying, with POST, to insert a new time object...what's wrong? I don't understand the error message! If I try to retrieve information with GET, with the same java class, it works! I hope you can help me, thx!
ps: sorry for my english.. ppss: maybe this forum got problem with rich text? I can't set [CODE] or similar tag in this post...
My Java knowledge is not very fresh so please bear with me. It looks like you're not sending any data to the server to create a new time object. In your code you've set Content-type to application/json, so you need to be sending JSON formatted data to the server. The following URL shows how to format your data (the example is in XML) when making a request:
Particularly, it looks like you need to be using an OutputStream to pass POST data to the server. After "my.addRequestProperty("Authorization", "Basic M2k1NnhzY3EzOG86WA==");" I would add something like the following:
OutputStream os = my.openOutputStream(); os.write( -- JSON encoded data -- );
Also, if you if you are using an important account (as opposed to a sandbox account), be sure to change your API token now that the base64 encoded version has been posted to this public forum.