2

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?

2
  • Have you tried logging the value of json? Commented Feb 25, 2014 at 1:40
  • @Spittal make sure your url is correct. From the document, it should end with .json because you use json request. https://{subdomain}.wufoo.com/api/v3/forms/{formIdentifier}/entries.{xml|json} Commented Feb 25, 2014 at 6:55

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Thanks, unfortunately I'm still getting the same error.
What's the service url you are attempting to do a POST?
1

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

Comments

1

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.

1 Comment

Hey! Thanks so much for the suggestion, I had actually tried this before to no avail but I've just tried again with this exact code and I'm still not seeing anything different from the previous error. Could there be something I'm missing?
0

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.

1 Comment

Keep in mind this isn't a real problem. Using BasicNameValuePair as I suggested that mean you don't send json string to server, so this header caused confusion for the server because server accept header application/x-www-form-urlencoded. You should read HttpPost document en.wikipedia.org/wiki/POST_(HTTP) for more detail. Therefore, your data send to server should look like this: Field17=Some+Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.