I have an requirement to pass a some values from mobile to server in a web service call and so I am planning to pass all the values in JSON format like the below
{ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "R&R" } ] }
The following is my service call
@RequestMapping("/saveName") @ResponseBody public String saveName(String acc) {jsonObject = new JSONObject(); try { ); System.out.println(acc); jsonObject.accumulate("result", "saved "); } catch(Exception e) { e.printStackTrace();jsonObject.accumulate("result", "Error Occured "); } return jsonObject.toString(); }
I am trying to call the above service by this way
localhost:8080/service/saveName?acc={ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "R&R" } ] }
But the output is like this
{ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "R
Can any body please tell me why I am not getting all the values please?
To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.
The transfer of parameters to the script can be done either directly or through a JSON or CSV file.
I would suggest to pass the JSON data in the body as a POST
request.But if you still want to pass this as a parameter in URL,you will have to encode your URL like below just for example:-
for ex json is :->{"name":"ABC","id":"1"}
testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D
for more information on URL encoding refer below
https://en.wikipedia.org/wiki/Percent-encoding
I know this could be a later post, but, for new visitors I will share my solution, as the OP was asking for a way to pass a JSON object via GET (not POST as suggested in other answers).
I have used this in some cases where I only can do GET calls and it works. Also, this solution is practically cross language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With