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?

Spittal
  • 779
  • 2
  • 12
  • 23
  • Have you tried logging the value of `json`? – Rajesh Feb 25 '14 at 01: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} – ductran Feb 25 '14 at 06:55

4 Answers4

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);
Juan Andrés Diana
  • 2,215
  • 3
  • 25
  • 36
1

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

arul
  • 190
  • 2
  • 15
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.

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
  • 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? – Spittal Feb 25 '14 at 19:32
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.

Spittal
  • 779
  • 2
  • 12
  • 23
  • 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 http://en.wikipedia.org/wiki/POST_(HTTP) for more detail. Therefore, your data send to server should look like this: `Field17=Some+Value` – ductran Feb 26 '14 at 03:24