JsonParseException: Invalid UTF-8 start byte 0x91 - Rest Template Spring boot - json

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.

Related

Pentaho HTTP Post multipart/form-data

I am trying to use HTTP Post to post the data by passing following Header and Body part,
Body:
One image URL having file path. Type as 'File'
Metadata in JSON format. Type as 'Text'
Headers:
Content-type = multipart/form-data
Authorization = Bearer 5412
Here is the screenshot of complete request,
This is how I have setup the HTTP Post task in Pentaho,
I found related post here using REST Client but this also didn't help: Pentaho HTTP Post using JSON
Really appreciate your help.
I think the problem is that "Post a file" doesn't mean include a file in the request, but rather to get the entire request from that file.
I don't know the specifics of how the data should look in the Post request, but the rough approach should be:
Pass the filename field to a Calculator step with the operation "Load file content to binary" to get a binary type field (ex: myimage)
Base64 (or other) encode the data with a Javascript step like this:
var encString = new Packages.java.lang.String( Packages.org.apache.commons.codec.binary.Base64.encodeBase64( myimage ) );
Wrap it in some content-type string indicating the encoding
Include the field in your request.
There is a patch on https://jira.pentaho.com/browse/PDI-14743 to enable proper binary transfer instead of String when using Rest Client.

Web Api 2 Content Negotiation not obeying Accept Header

Following Darrel Miller's guide I'm posting raw data to my web api controller and persisting it. The content could be application/xml or application/json.
In the corresponding get method I retrieve the posted content, parsing to either XElement for XML content or JToken for json and returning OK(json) or OK(xml).
public IHttpActionResult Get()
{
// obtain parsed JToken or XElement
return OK(parsedObject);
}
The problem is that this doesn't obey the Accept Header, for example returning the original json when Accept is "application/xml". Is this by design or am I missing something?
I'm expecting this kind of behaviour.
Edit - the Accept header is obeyed if I amend the content passed to the OK method to parsedObject.ToString(), so there seems to be an issue with converting the JToken object to XML.
Are you sure you didn't remove the XmlSerializer from your formatters list?
Anyway you can always control the formatter that will be used for serialization at response level.
For example:
//forcing xml
HttpResponseMessage resp = Request.CreateResponse(HttpStatusCode.OK, result, new XmlMediaTypeFormatter());
return resp;
You can change the new XmlMediaTypeFormatter() with the instance of your current formatter for your actual configuration.
Check this link and this link for more details.

Netty 5 sending JSON POST request

I'm trying to send JSON content to the remote endpoint as a POST request over HTTPS using Netty 5.0.0.Alpha2. My problem is that the new API doesn't seem to offer a way of simply setting the JSON content as the request body without using an HttpPostRequestEncoder object and specifying a name for the JSON attribute. Is it possible to set the content "anonymously", and if so, how?
I was able to make the request successfully by using the concrete DefaultFullHttpRequest handle instead of the HttpRequest interface. The code now looks a bit like this (edited for brevity):
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/path/to/x");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);
request.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
request.headers().set(HttpHeaderNames.ACCEPT, "application/json");
request.headers().set(HttpHeaderNames.HOST, "somehost.com:443");
ByteBuf buffer = request.content().clear();
int p0 = buffer.writerIndex();
buffer.writeBytes(json.getBytes());
int p1 = buffer.writerIndex();
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0));
I'm sure this could be prettier (in particular the content-length calculation), but it seems to work and saves anyone wasting any brain cycles on this question.
Thanks if you happened to consider it.

Handling special characters in WebClient uploadString method using JSON

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.

Test jsonp rest app response using httpbuilder

Hi
I'm trying to build up a rest Service which provide JSON response.
Yesterday i figured about what is JSONP, why to use it an so and so.
Today i would like to make some test with my new version of my service.
To test it, i use HTTPbuilder. But i can't get it working correctly.
Here is the start of my test:
def client = new RESTClient("http://localhost:"+SERVER_PORT)
def resp = client.get(path:'/music', contentType:JSON);
And here is the error i got:
ATTENTION: Error parsing 'application/javascript' response
net.sf.json.JSONException: Invalid JSON String
How can i help httpbuilder to understand that the response is a JS callback?
Ok i was a littel dumb on this one.
The accepted content type must be ContentType.ANy