2

I want json to be sent to GET request in query parameter to get the response for that json request.

If I use a link something like this :

www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}

Then the url part appears blue if I do it as sysout statement, something like this:

www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}

and the json appears as if it is not the part of the request.

To create a URL, I'm appending the manipulated JSON string to the URL and then sending the request. But it appears as two different strings.

Also I have used encoder to encode the JSON part

filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}

In that case the brackets and double quotes everything in that json is encoded, even the equalTo sign. Also the link appears blue but while sending request it throws exception of 400 Bad Request, since the equalTo is also converted to its encoding format.

I tried encoding only the JSON part leaving the filter= in the url itself, something like this :

www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}

The results that appear after the request is send is different from the results I want.

I'm using following code to create a JSON:

private String getVinFromInventoryRequest(String vin) throws JSONException {
    JSONObject request = new JSONObject();
    JSONArray orArray = new JSONArray();
    for(String vin : vins) {
        JSONObject termsObject = new JSONObject();
        JSONObject vinsObject = new JSONObject();
        JSONArray vinsArray = new JSONArray();
        vinsArray.put(vin);
        vinsObject.put("vin", vinsArray);
        termsObject.put("terms", vinsObject);
        orArray.put(termsObject);
    }
    request.put("or", orArray);
    System.out.println("OfferMapper.getVinFromInventoryRequest " + request.toString());
    return request.toString();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Priyanka Naik
  • 344
  • 6
  • 13
  • How do you encode the json? What is the result of that encode? Could you provide example? – Mark May 31 '16 at 10:38
  • duplicate of : http://stackoverflow.com/questions/23476033/how-should-i-put-json-in-get-request – Pankaj Verma May 31 '16 at 10:41
  • I encode the json using URLEncoder in java. It is encoded in this format : filter%3D%7B%22and%22%3A%5B%7B%22terms%22%3A%7B%22vin%22%3A%5B%221g1105sa2gu104086%22%5D%7D%7D%5D%7D – Priyanka Naik May 31 '16 at 10:44
  • Instead of creating each and every json string with some humar err. I'm using JSONObject and JSONArray as required. Check the code in the question for creating json. – Priyanka Naik May 31 '16 at 10:49
  • I get following error : Unexpected character ('\' (code 92)): was expecting double-quote to start field name at [Source: {\"or\":[{\"terms\":{\"vin\":[\"1g1105sa2gu104086\"]}}]}; line: 1, column: 3] if I try using the solution @PankajVerma gave in the link. – Priyanka Naik May 31 '16 at 13:13
  • The request sent is something like this : – Priyanka Naik May 31 '16 at 13:13
  • The request sent is something like this : http://url.com/search?context=vehicles&filter=%7B%5C%22or%5C%22%3A%5B%7B%5C%22terms%5C%22%3A%7B%5C%22vin%5C%22%3A%5B%5C%221g1105sa2gu104086%5C%22%5D%7D%7D%5D%7D&responseFields=vin,inventoryOwner and using this I'm getting 400 Bad Request – Priyanka Naik May 31 '16 at 14:50
  • The 2nd approach in your solution i.e /*I tried encoding only the json part leaving the filter= in the url itself, something like this : www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]} The results that appear after the request is send is different from the results I want.*/ one more thing did you set the Http request content type to 'application/json' like : request.addHeader("content-type", "application/json"); – Pankaj Verma Jun 01 '16 at 05:41

1 Answers1

0

Also look what I found with a little googling :

JSONObject json = new JSONObject();
json.put("someKey", "someValue");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://yoururl");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}

For more info see : HTTP POST using JSON in Java

Community
  • 1
  • 1
Pankaj Verma
  • 191
  • 10