IE - Angular's json response contains unicode character that removed additional characters. Invalid JSON - angularjs

I have a http response that has a name that contains a unicode character (ex. Müller).
In IE11, I'm getting an error that says "Invalid Character" because it seems that in IE11 the whole http response is read as a string in angular's http response, and it tries to parse this string into JSON (instead of already in JSON format). But in the JSON string it looks something like this:
...,\"lastName\":\"M�}],\"id\":1,...
The problem is that part of the last name got stripped, and now the lastName value has a missing close quote. I don't mind that its displaying the diamond question mark, its just completely breaking the response now.
In chrome it works fine, as the data is actually returned as a JSON object, unlike IE11 where it returned as a string, and then tries to convert to JSON in the default transform response functions.
The request is in application/json charset: utf-8 format.
The response is in application/json format.
Does anyone know whats wrong?
Edit: In IE11's network response body, it shows it correctly as "Müller" in the JSON format.
Edit: It seems like its eating up the first 5 characters after the ü when returning the response. (ex. Mülleraa will look like ...\"M�a\"... where the closing quote is back with an additional 'a' char)

In your request, add:
headers: {
"Accept": "application/json;charset=utf-8",
"Accept-Charset":"charset=utf-8"
},

Related

Azure Logic App post request always return Bad Request

When call API with Content-Type: as application/x-www-form-urlencoded I always get Bad request(Status code 400).
If I paste same details in Postman it's work without any issue.
I am assuming body must be formatting before sending request!
Update 1
I need to add prefix updateorder=" somehow in front of json payload and Json payload should not convert to text.
if I use concat OR put updateorder= just befor json payload its convert into text as per below and request is invalid.
Using replace from "\"" to "" does not work as well as its again convert json payload into text so request is invalid in this scenraio as well.
I have created logic app with your requirements.
Here is reference image for that:
I call the API in content-type using your code body, I also got same error code.
Case-1:
I tried with
{
"updateOrder": {
"id": "1",
"orderDetails": {
"name": "sam",
"sal": "1000"
}
}
}
It worked successfully for me I got success code 201, and it is not converting into text also.
Here is the reference image:
So, you can try these changes in your BODY After updateorder instead of equals to (=) you should try semi colon (:).
Case-2:
After trying Case-1 still you're getting error once check this and try if you are trying to get any token for authentication and if you are sending credentials in body then use this
Instead of Content-Type use "Accept" (Enter key) and instead of application/x-www-form-URL encoded use "* / *"(Enter value)
Enter Key: Accept
Enter value: */*
Here is the reference image for that:

Best method to send a complex json request with Convertigo

I have a complex json request that I'd like to POST to an api but I can't find the proper way to do this inside Convertigo Studio. Could anyone please indicate me the best way to do this?
Here is the request I'm send through curl that gives me the results. These result will be used by the front to display data.
curl -k -H "Accept: application/json" --compressed -XPOST https://myserverurl/api/search -d #- << EOF
{
"api-key":"somekey",
"usage":"someusage",
"criteria":{
"timestamp":{"from-to":{"date-pattern":"yyyy/MM/dd-HHmmss","from":"2019/07/28-000000","to":"2019/08/27-235959"}},
"timestamp-field":"timestamp",
"metric":"*",
"filter":{
"and":
[
{"eq":{"attribute":"type","value":"sometype"}},
{"simple-query":{"query":"_exists_:city"}},
{"neq":{"attribute":"status","value":"1"}}
]
}
},
"info":"someinfo",
"size":10000,
"mode":"last-hits",
"format":{"tabular":{"columns":["col1", "col2","col3"],
"last-hits-columns":["name"],"order-by":[{"name":"name","direction":"ASC"}]},
"timestamp":{"date-pattern":"dd/MM/yyyy HH:mm:ss"}},
"index":"someindex",
"last-hits-count":"1"
}
EOF
I now would like to transpose that into a Convertigo approach using the proper connector and transactions but so far I'm hitting a wall. Any help is appreciated.
update: So I've managed to contact the API, i.e. reproducing the first part of the curl, by implementing a HTTP_Connector and then a HTTP_Transaction. The server is answering in the expected way.
Now what I can't do is posting the json string. I've tried implementing a http_single_variable which default value is that json string but it doesn't work, I get the following error:
HTTP result {ContentType: application/json, Length: 277}
{"error":{"request":"http://localhost:8550/api/search","message":"Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.InputStreamReader#6c195833; line: 1, column: 2]","target":"/search"}}
The error seems to be coming from the header, which has been defined as Accept, application/json. When I remove it I get a HTTP 500 Error from the server.
To post a JSON body in a Convertigo request you have to add a variable "__body" to your transaction:
HTTP single-valued variable
If your API returns a JSON response, you should use a JSON_HTTP_transaction instead of your HTTP_Transaction transaction.
Set "HTTP verb" property transaction to POST and "HTTP headers" property to "Content-type, application/json".
The value of the __body variable is set in a sequence by a Sequence_JS step like this:
var data = {
"param1": "value1",
"param2": "value2",
...
};
Then, use a jElement step to transform "data" to a JSON string source:
JSON.stringify(data)
in "Expression" property.
Next step is the Call of your transaction. In the __body Source point to the jElement text.
Here is a link to a Convertigo (7.5.7+) sample:
useBody.car
Hope that Helps.

React Fetch POST removes characters with header Content-type = application/x-www-form-urlencoded

I am using react with fetch for sending an image to the server.
I have tried using Content-type = application/x-www-form-urlencoded to pass my data to the server but it replaces special characters with spaces (i.e. + becomes a space).
I have switched the header to be Content-type: multipart/form-data but that throws the error
Failed to load resource: the server responded with a status of 500
(Internal Server Error).
I have added a boundary to the Content-type as boundary=abcdefg.
That did not change anything and I am not sure what my boundary would be.
Finding a clear answer with straight forward examples about boundaries have been impossible to get.
The data that I am sending is a large string.
If needed I can post that as well.
Here is a sample of the code that is causing the problem:
SaveTest4(data: string) {
const options = {
method: 'post',
headers: {
"Content-type": "multipart/form-data; boundary=abcdefg"
},
body: 'data=' + data
}
fetch('api/DataPoint/AddTest4', options);
}
Based on part of your analysis, it sounds like you're trying to send base64-encoded data. A content type of application/x-www-form-urlencoded will result in the server performing URL decoding, which will replace each instance of + in the content body with a space character.
When you used a content type of multipart/form-data, the server fails with status 500 because the data you provided wasn't a properly constructed MIME document.
My psychic debugging powers tell me that you're trying to post a base64-encoded file to a ASP.NET MVC WebAPI endpoint that's expecting a JSON document. You might have a controller method that looks like this:
[HttpPost("api/DataPoint/AddTest4")]
public void AddTest4([FromBody] string data) { ... }
If you send with a content type of application/json, this endpoint will expect a document that looks like this:
"{base64-encoded-data}"
Note that there are quotes around the data, because a quoted string is a proper JSON document. You'd just use JSON.stringify() to create the quoted string in this case, which would escape any quotes within the string correctly.
If you send with application/x-www-form-urlencoded, you'd need to send a document that looks like this:
data={base64-encoded-data}
But, as you note, you'd have to make sure you escape all of the special characters in the payload; you can do this using window.encodeURIComponent(), which would translate each "+" to "%2B", among other things.
If the files that you're uploading to this endpoint are large, it would be significantly better to use an instance of FormData. This would allow the browser to stream the file to the server in chunks instead of reading it into memory and base64-encoding it in JavaScript.

Angular http.get charset

I'm having some trouble getting my angular application to parse my json-data correctly.
When my json-feed contains e.g. { "title": "Halldórsson Pourié" }
my application shows Halld�rsson Pouri�
I figure the problem is the charset, but i can't find where to change it.
Currently i am using ng-bind-html and using $sce.trustAsHtml(), and I'm very sure the problem occurs when $http.get(url) parses my json.
So how do i tell $http.get(url) to parse the data with a specific charset?
I had a similar issue and resolved it by using:
encodeURIComponent(JSON.stringify(p_Query))
where p_Query is a JSON containing the details of the request (i.e. your { "title": "Halldórsson Pourié" }).
EDIT:
You may also need to add to the header of your GET request the following:
'Content-Type': 'application/x-www-form-urlencoded ; charset=UTF-8'
Had same problem with accented characters and some scientific notation in text/JSON fields and found that AngularJS (or whatever native JavaScript XHR/fetch functions it is using) was flattening everything to UTF-8 no matter what else we tried.
The other respondent here claimed that UTF-8 should somehow still be accommodating your extended character set, but we found otherwise: taking samples of the source data directly, we loaded it into text editor with different encodings and UTF-8 would still squash extended characters to that � placeholder.
In our case, the encoding was ISO-8859-15 but you may be sufficient with ISO-8859-1.
Try adding this $http configuration to your $http.get() call, accordingly:
$http.get(url, {
responseType: 'arraybuffer',
transformResponse: function(data) {
let textDecoder = new TextDecoder('ISO-8859-15'); // your encoding may vary!
return JSON.parse(textDecoder.decode(data));
}
});
This should intercept AngularJS default response transformation and instead apply the function here. You will, of course, want to consider adding some error handling, etc.
If there are better ways to do this without a custom transformResponse function, I have yet to find anything in the AngularJS documentation.
$http.get(url, {responseType: 'arraybuffer', }).then(function (response) { var textDecoder = new TextDecoder('ISO-8859-1'); console.log(textDecoder.decode(response.data));});
This code will replace all your � placeholders into normal characters.

WebApi and Ampersands in name

So my angular website has a webapi with the following method.
[Route("items/{itemName}")]
public object GetMcguffinsByItem(string itemName)
{
return _mcguffinsService.GetAllByItemName(itemName);
}
However, an item name can have an ampersand as a valid character. However when attempting to use items that do have an ampersand, the method will return a 400 badrequest.
Im not sure how to go about fixing this problem.
For more verification: I was under the impression that encoding and using %26 is all required to pass an ampersand to part of the URI. It seems to be a common answer when searching my problem. I have excluded the angular as I can verify that it builds the string correctly, and other names produce the desired result.
The javascript method encodeURIComponent() followed by using the angular service double encodes the item name, and returns a 404.
EDIT:
Sample Input:
A&B 266
After Encoding:
A%26B%20266
Console:
angular.js:10722 GET http://localhost:60894/api/v1/mcguffins/items/A%26B%20266 404 (Not Found)
Using the browser on api directly with same input gives this error:
[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (&).]
System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +11944671
System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +55

Resources