I am using the WebClient.UploadString method to send a JSON object to a web service.
This is all working fine until the contents of the serialized JSON object contains a special character e.g. é, è, ê, ë, à or û
The error I get is a HTTP 400 on the receiving web service end and looking through logs shows that the entire json string is missing from the requests, almost like it fails to get sent.
Here is the code I am running to send the request:
var json = JsonConvert.SerializeObject(item, Formatting.Indented);
var response = WebClient.UploadString(GetRequestUri(partialUrl), "POST", json);
var result = JsonConvert.DeserializeObject<T>(response);
So far I've tried:
urlencoding the entire string but this produces an error because the preceding "{ is also encoded (i think).
Setting the WebClient encoding to UTF8 which makes no difference (I think this is default anyway).
Any help of suggestions would be appreciated.
Thanks in advance.
Related
This is a question that has been asked on Stackoverflow several times. But I could not found a solution for my specific scenario.
In all those cases, the solution is to save the json data in UTF-8; so that the caller gets a valid Json.
In my case with Spring boot and RestTemplate, I get a Json as a response for a API request I make to a third party server. I have no control whatsoever to change that side.
So, Is there any way that I can make something from myside as the receiving end to fix following issue.?
JsonParseException: Invalid UTF-8 start byte 0x91
Below is how I have coded my request.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
----
----
HttpEntity<HashMap> request = new HttpEntity(dataMap, headers);
Thank you..!
I got it resolved.
I had to accept the response as a String first and then do the string manipulation accordingly as per the format I want the data to be.
String responseJsonString = restTemplate.postForObject(url, request, String.class);
Hope it might be of help for somebody.
I am writing an automation tool, that mostly send requests and get JSON responses from a server. When I run my code directly from IntelliJ - I get a proper response. But, when I run my program from the console there is a problem. Special Spanish or French chars are being displayed in a wrong way.
For example:
We’ve
My code:
RestResponse restResponse = restRequest.sendRequest();
JSONObject jsonResponse = restResponse.getResponseJson();
What may be the cause for this error and how to get the foreign language chars to appear as they should?
The error might be cause by the character encoding, in IntelliJ you probably have already defined character encoding but not in the console.
So you can define the console encoding to UTF8 :
Console.OutputEncoding = Encoding.UTF8;
You can find some hint there :
- How to get a UTF-8 JSON
- Encode String to UTF-8
- System.out character encoding
I've been looking through various solutions for converting JSON responses to string, but I'm still getting something wrong.
I'm trying to read the response from a post request, convert it to a string and modify it (using groovy), then pass it on the next request. The problem seems to be in the groovy script, it's not reading in the response from the first HTTP request and I'm not sure how to fix that.
Let's say I ask Mailchimp for subscriber data, and they send back an http request with JSON in the body. Should I be able to go:
var thingy = body.property;
Or should I have to go:
var object = JSON.parse(body);
var thingy = object.property;
?
Also, does the node.js body parse parse JSON for me?
JSON is sent over the wire from the server as a string. That's what JSON is - a string format.
Whether or not it arrives at your code as a string or as already parsed Javascript object depends entirely upon the code you are using to make the http request and perhaps what headers the server sets and what auto-detection the code doing the Ajax call makes.
If the response header sets the type to json, then some code making the request will automatically parse it for you into Javscript. Other code will leave that to the caller to do. If the server does not set the proper headers, then some code will auto-detect it as JSON and parse it and other code will not.
So ... bottom line. It depends entirely upon what the server is doing in its response and what code is being use to make the request. You can very easily just do a console.log(body) and see whether you have a JSON string or an already parsed Javascript object.
If you really weren't sure what behavior you would get, you can test the type and act accordingly (though a given server and calling code should be consistent so you shouldn't have to vary your behavior) once you test how it behaves.
if (typeof body === "string") {
body = JSON.parse(body);
}
Depends on the API, usually you get the response header Content-type: application/json. If that is the case there's probably no need to parse the response as most of the clients will understand that it's a json object and parse it for you. Anyhow, not all clients will do this automatically.
I'm building an application that communicates with a django backend using json-rpc. So far all has been working well. However I've found an anomaly in sending " ". As far as I know the request works fine, however django interprets the response badly. I've reproduced a simplified request and response below:
Request:
{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}
Django receives:
<QueryDict:u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello ': [u''], u'nbsp': [u''], u'there"}}': [u'']}>
Expected response:
<QueryDict: {u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}': [u'']}>
It seems like django interprets the & and the ; as special characters and so creates an unexpected dictionary in its request.POST variable.
What do I need to do to make sure that the json string doesn't get malformed? I have tried encoding it using the php htmlspecialchars() method, but since that doesn't remove the '&' the problem persists.
Any help will be much appreciated.
Django is handling the (POST?) request by decoding the body (your json string) as if it were a query string, and not a json.
Within a query string, & and ; denote the end of a key:value pair. Splitting up your request body on those two characters yields the key:value pairs you see in the Django QueryDict.
You need to get hold of the POST request body and explicitly decode it to a dict yourself using either the standard lib json, or simplejson module.
I have little experience with Django specifically, but I imagine that somewhere in your view handler you would do something akin to:
try:
data = json.loads(requesst.raw_post_data)
## work with the data...
except ValueError:
## do something...
No doubt Django provides a way to move this json handling out of your views, and to somewhere more suitable.