JSON in POST request

edited January 2017 in Library Questions

First off I'm sorry if this has already been asked or answered. I have tried searching here but haven't found anything that answers it adequately enough for me to figure out my problem. So there is a couple servlets on the web that I have been given access to in order to make a program that will generate stats based on the data. As this has been given to me out of the kindness of their hearts I would rather not bother them with my inadequacies as a coder. So now onto my question...

All requests must include a POST parameter called “json” containing a JSON representation of the data to send to the servlet. All requests must look like this:

json = { 
  "version" : "xx",
  "game" : "xxxxxxxxxxxxxx",
  "platform" : "xxxxxxxx",
  "userId" : <your user id>,
  "password" : <your password>,
  "data" : { <json data to send to the specific servlet> }
}

In Processing I have downloaded the HTTP Request library by Rune Madsen and Daniel Shiffman. I guess my main problem is I am unsure as to the correct way to send JSON in the POST request. Any help would be greatly appreciated. Thank you.

Answers

  • the official library allows you to attach a json file so you could write out the json and attach that. bit long winded.

    i forked the library to allow people to do it a bit more directly, see the project readme. source is here. but it doesn't have the last 10 updates to the original so how useful it is now is debatable.

    https://github.com/acdean/HTTP-Requests-for-Processing

    iirc it's not usable as a library but if you copy the java file into your project then you can use the PostRequest class.

  • All requests must include a POST parameter called “json” containing a JSON representation of the data to send to the servlet.

    oh, if this is all you want then i think you can just use the original library and addData("json", "whatever");

  • Thank you for the response koogs. Before I mess with all the other stuff I'm trying the addData function. My question is addData takes two strings in double quotes but the JSON has double quotes as well. So I've tried replacing all the JSON info with single quotes and I've tried escaping all JSON double quotes \".

  • show us what you've tried. tell us what's wrong.

    single quotes won't cut it, i'm afraid.

  • edited January 2017
    PostRequest post = new PostRequest("http://www.XXXXXXXX.net/XXXXXX/getcurrentround");  
    post.addData("json","\"version\":\"XXX\",\"game\":\"XXXXXXXXXXX\",\"platform\":\"XXXXXXXXX\",\"userId\":\"XXXXXXXXX\",\"password\":\"XXXXXXXXXXXXXX\"");
    post.send();
    println("Reponse Content: " + post.getContent());
    println("Reponse Content-Length Header: " +  post.getHeader("Content-Length"));
    

    this is in my setup function and the output I get is:

    Reponse Content: {"serverTime":1484260680254,"result":"WRONG_PARAMETERS","errorMessage":"Bad JSON, mapping error: Can not deserialize instance of org.codehaus.jackson.node.ObjectNode out of VALUE_STRING token\n at [Source: java.io.StringReader@1b59a5f; line: 1, column: 1]\njson = \"version\":\"XXX\",\"game\":\"XXXXXXX\",\"platform\":\"XXXXX\",\"userId\":\"XXXXX\",\"password\":\"XXXXXXXXXX\""} Reponse Content-Length Header: 437

  • edited January 2017

    OK, try this

    Use \\" to escape your "s. I think the single \ is being interpreted by the Java compiler as escaping the " in the code and doesn't end up in the string... Hard to explain, but try it.

    (Ha, the forum also eats \s)

  • unfortunately replacing the \" with \" gives mean error

    processing.app.SketchException: unexpected char: '\' at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:361) at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:155) at processing.mode.java.JavaBuild.build(JavaBuild.java:122) at processing.mode.java.JavaBuild.build(JavaBuild.java:104) at processing.mode.java.JavaMode.handleLaunch(JavaMode.java:126) at processing.mode.java.JavaEditor$33.run(JavaEditor.java:1068) at java.lang.Thread.run(Thread.java:745)

    koogs, I do just want to thank you again. Even though it's not working yet I really do appreciate you trying to help me. Thank you.

  • well the forum ate up the double backslashes. I did replace the single backslash double quotes with double backslash double quotes. and that is the error I got.

  • Actually I got it working... I put the whole thing into a string variable and printed it out to see if it was printing out correctly and it was but I was still getting errors. So what I did was encapsulate the whole thing in brackets {} then I just got an error saying old version so I added a couple numbers to it and now i get some data. So now all the real work begins... Thank you koogs. I couldn't have done it without you.

  • edited January 2017

    Test: //"" and //" and /" and /"".

Sign In or Register to comment.