Spittal
Spittal

Reputation: 779

Posting to REST api using Java, specifically Android

I'm posting to the Wufoo api inside of an Android app and I am hitting a bit of a snag. My data does not seem to be formatting in a way that the server likes (or there is some other issue). Here is my code (note authkey and authpass are placeholders in the exmaple):

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

String json = "";

JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Field17", "Some Value");

json = jsonObject.toString();

StringEntity postData = new StringEntity(json, "UTF8");

httpPost.setEntity(postData);

String authorizationString = "Basic " + Base64.encodeToString(
("authkey" + ":" + "authpass").getBytes(),
Base64.NO_WRAP);

httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Authorization", authorizationString);

HttpResponse httpResponse = httpclient.execute(httpPost);

inputStream = httpResponse.getEntity().getContent();

The response I get back from the server looks like this:

{"Success":0,"ErrorText":"Errors have been <b>highlighted<\/b> below.","FieldErrors":
[{"ID":"Field17","ErrorText":"This field is required. Please enter a value."}]}

This is the response for a failure (obviously) which leads me to believe I'm doing the authentication correctly, and that it just doesn't like my JSON string, I've looked through the API docs which are located here: http://www.wufoo.com/docs/api/v3/entries/post/

and by all accounts this should work? Any suggestions?

Upvotes: 2

Views: 950

Answers (4)

Spittal
Spittal

Reputation: 779

I actually figured this out, this isn't a problem with the code anyone here gave me, it's the fact that I was sending the wrong header info. This must be a quirk of the Wufoo API.

If I use the BasicNameValuePair objects like what was suggestion by R4j and I remove the line

httpPost.setHeader("Content-type", "application/json");

everything works perfectly!

Thanks for all the help and I hope this helps anyone who is having trouble with the Wufoo API and Java.

Upvotes: 0

ductran
ductran

Reputation: 10203

After reading the document, I think you missed the point. The server accepted fields parameter from http post, not from a json string.
Your problem looks like this one. So your request should like this:

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Field17", "Some Value"));

httpPost .setEntity(new UrlEncodedFormEntity(postParameters));

Hope this can help.

Upvotes: 1

arul
arul

Reputation: 300

The Field17 may be of specific field type other than string.

Upvotes: 1

Juan Andr&#233;s Diana
Juan Andr&#233;s Diana

Reputation: 2225

I would start by looking at this line:

StringEntity postData = new StringEntity(json, "UTF8");

It's "UTF-8", not "UTF8".

Note: I would suggest you using the HTTP.UTF_8 constant in order to avoid this kind of problem again.

StringEntity postData = new StringEntity(json, HTTP.UTF_8);

Upvotes: 1

Related Questions