0

I am sending a post request in Android to parse.com and need to include the headers and body in a single json object as the post request body. Is this possible like this (most likely not since I get error message from parse.com response:

{
"headers": [
    {
"Content-Type":"application/json",
"X-Parse-Application-Id":"key1",
"X-Parse-REST-API-Key":"key2"

"body": [
    {
        "ephrase": "When the going gets tough, the tough get going.",
        "nvote": 154,
        "pphrase": "Meaning2",
        "yvote": 364
    },
    {
        "ephrase": "Beggars can't be choosers.",
        "nvote": 1,
        "pphrase": "meaning1",
        "yvote": 8
    }
] } 

If this is not going to work, I am trying the following but still get an error "107 invalid utf-8 tsring was provided" from parse.com

I include this in header section of the picture:

"Content-Type":"application/json",
"X-Parse-Application-Id":"key1",
"X-Parse-REST-API-Key":"key2"

And this in the body section where parse.com expects a json object

{ [
    {
        "ephrase": "When the going gets tough, the tough get going.",
        "nvote": 154,
        "pphrase": "",
        "yvote": 364
    },
    {
        "ephrase": "Beggars can't be choosers.",
        "nvote": 1,
        "pphrase": "",
        "yvote": 8
    }
] }

enter image description here

4
  • From where are you sending this request? What client are you using to make this call? Commented Dec 31, 2015 at 4:46
  • You have posted wrong json struct above. please sure it is extractly Commented Dec 31, 2015 at 4:47
  • Why do you need to send the keys in the header? Those should be secret to your application. Commented Dec 31, 2015 at 4:49
  • Parse requires you to send your keys in the header Commented Dec 31, 2015 at 4:51

4 Answers 4

1

Adding headers in the body is not good practice, headers needs to be sent separately.

If you are using HTTPPost to send the request then you can add the headers using addheader function

httppost.addHeader("YOUR-KEY", "YOUR-value");

in your case this will be

 httppost.addHeader("Content-Type", "application/json");  
 httppost.addHeader("X-Parse-Application-Id", "key1");  
 httppost.addHeader("X-Parse-REST-API-Key", "key2");  

Hope this will help you

Sign up to request clarification or add additional context in comments.

Comments

0

Headers are not part of the JSON body. Headers are separate part of the request. Only the body content should be part of the json you send.

1 Comment

I have a small code whch also allows me to include separate headrers and bod, would you guys know how that may work.
0

I recommend u to use HTTPURLConnection instead of other deprecated apis.

    String urlStr = "";
    HttpURLConnection conn;
    URL url1;
    String jsonBody = "{ [\n" +
            "    {\n" +
            "        \"ephrase\": \"When the going gets tough, the tough get going.\",\n" +
            "        \"nvote\": 154,\n" +
            "        \"pphrase\": \"\",\n" +
            "        \"yvote\": 364\n" +
            "    },\n" +
            "    {\n" +
            "        \"ephrase\": \"Beggars can't be choosers.\",\n" +
            "        \"nvote\": 1,\n" +
            "        \"pphrase\": \"\",\n" +
            "        \"yvote\": 8\n" +
            "    }\n" +
            "] }";

    try {

        JSONObject jObj = new JSONObject(jsonBody);

        url1 = new URL(urlStr);
        conn = (HttpURLConnection) url1.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("X-Parse-Application-Id", "key1");
        conn.setRequestProperty("X-Parse-REST-API-Key", "key2");
        conn.setDoOutput(true);
        conn.connect();

        OutputStream os = conn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write(jObj.toString());
        osw.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Just make sure u do above things inside AsyncTask's doInBackground() or a thread will just be fine.

4 Comments

Please remove this answer if you are not looking to solve the problem seriously.
@Amir: I may have misunderstood ur Q. Wts goin wrong could u plz tell me?
@Amir: Did it helped u?
Going for new year fireworks :) will let you know tomorrow. Thanks anyway
0

I found this sample that has all types of HTTP:

https://github.com/loopj/android-async-http

If you are using this you need to add the following code to JsonStreamerSample.java to allow you including Headers.

public boolean isRequestHeadersAllowed() {
    return true;
}

I still have a small problem though which is further explained in the question below.

Passing JSON object to parse.com in Android

Comments

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.