JSON handling of POST


#1

I have a report object that I've serialized to JSON that I'm trying to submit, and no matter what I do I get:

{"code":"001","message":"Missing Parameter - task."}

Which I can't get past even if the entire entity is just {"task:report"}. Is there some other JSON container that I should be putting the parameters into for Ushahidi to be able to read them?


I'm able to post using regular html form (application/x-www-form-urlencoded), but it's far less convenient to build the required string.



#2

could you post how you are passing the form variables to the api?



#3

I'm having trouble posting my code, getting a 404 forbidden. Trying just a plain old post right now.



#4

Grrr... it is a 403 forbidden, I've tried taking out parts of the code text the forum might not like, but I'm going to have to try it piecemeal. Sorry in advance for the multiple posts.


Thanks for getting back to me. We are using the Apache HTTP Client. We create our post as shown below. Note that the payload is generated by Jackson serialization, but the string is exactly what it produces. Let me know if you see any issues. As I said the url encoded post with basically the same information works fine.



#5

First string of code:


Code:
URI uri = new URI("http://OUR_DEPLOYMENT.crowdmap.com/api");


#6

Next line:


Code:

String payload = "playlod = {"task":"report","incident_title":"Test","incident_description":"new test","incident_date":"08/03/2011","incident_hour":"4","incident_minute":"32","incident_ampm":"pm","incident_category":"1","location_name":"Aptima","person_first":"Joe","person_last":"Smith","latitude":"42.183019","longitude":"-71.756945","resp":"json"};


#7

Next set of lines:


Code:

String type = "applicaiton/json";
HttpClient client = new DefaultHttpClient();


#8

Next set:


Code:
StringEntity stringEntity = new StringEntity(payload, type, "UTF-8");
HttpPost httppost = new HttpPost(uri);


#9

Strange, so it looks like the 403 error might be from my last few lines of code. In broken form they are:


httppost dot setEntity open-paren stringEntity close-paren


HttpResponse response = client dot execute open-paren httppost close-paren



#10

So all the code together:


Code:
String payload = "playlod = {"task":"report","incident_title":"Test","incident_description":"new test","incident_date":"08/03/2011","incident_hour":"4","incident_minute":"32","incident_ampm":"pm","incident_category":"1","location_name":"Aptima","person_first":"Joe","person_last":"Smith","latitude":"42.183019","longitude":"-71.756945","resp":"json"};
String type = "applicaiton/json";

HttpClient client = new DefaultHttpClient();
StringEntity stringEntity = new StringEntity(payload, type, "UTF-8");

HttpPost httppost = new HttpPost(uri);


httppost dot setEntity open-paren stringEntity close-paren

HttpResponse response = client dot execute open-paren httppost close-paren


Sorry for the confusion, I guess the forum tries to actually execute the last part?