1

Good day, been struggling a bit with this and not sure what to do anymore. I am using the android Asynchronus Http Library here to send a POST to parse but i keep getting this error message.

 {"code":107,"error":"This endpoint only supports Content-Type: application/json requests, not application/x-www-form-urlencoded."}

here are the relevant parts of my code:

           client = new AsyncHttpClient();


        client.addHeader("Content-Type", "application/json");
        client.addHeader("X-Parse-Application-Id", context.getResources().getString(R.string.app_id));
        client.addHeader("X-Parse-REST-API-Key", context.getResources().getString(R.string.rest_api_key));


    RequestParams params = null;
    JSONObject json = new JSONObject();

           try {
                    json.put(NAME, player_text.getText().toString());
                    json.put(CLUB, club_text.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

   params = new RequestParams("json",json); //takes a key value pair

   client.post(context, url, params, responsehandler); // posting to https://api.parse.com/1/classes/ABCDER/

i have set the header content-type to application/json so i don't know why i am getting the error. i have also requested the getContentType from the params to see and it gives me the error PARSE was complaining about. What could i be doing wrong? Any help is highly Appreciated. Thanks in advance

irobotxx
  • 5,963
  • 11
  • 62
  • 91

1 Answers1

0

The JSON needs to be posted as the data/body, not encoded in the URL parameters.

Here's another answer that shows how: POSTing JSON/XML using android-async-http (loopj)

So for you:

JSONObject json = new JSONObject();

try {
    json.put(NAME, player_text.getText().toString());
    json.put(CLUB, club_text.getText().toString());
} catch (JSONException e) {
    e.printStackTrace();
}

StringEntity body = new StringEntity(json.toString());

client.post(context, url, body, "application/json", responsehandler);
Community
  • 1
  • 1
Timothy Walters
  • 16,866
  • 2
  • 41
  • 49