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>
The request is done by servlet, in this way:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
HttpURLConnection my = null;
URL myint = new URL( -- query url --);
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...
Hey pennywise,
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:
https://www.myintervals.com/api/resource.php?r=time
The following URL contains an example of how to format your request to make a POST request using JSON data:
https://www.myintervals.com/api/examples.php
I've done a little bit of research on doing this in Java and this page looks like a good place to start:
https://developers.sun.com/mobility/midp/ttips/HTTPPost/
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.
Hope this information helps.
Cameron Brooks
Pelago
Thank you, now it works!
In detail, if someone else need, the code is
(...)
my.setDoOutput(true); //this to allow URL Connection to send output
OutputStreamWriter osw = new OutputStreamWriter(my.getOutputStream());
osw.write( /* JSON Content */);
(...)
For the token, no problem about it, I'm using a free account for testing purpose, but thanks for your advice.
Thank you again, regards!
1 to 3 of 3
Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com