JSON.stringify URLEncoding my JSON cookie causing "Invalid JSON primitive" - json

I have a site cookie where the value is a JSON object. I serialize this to a specific type in my server code and back to JSON when passing back as a cookie. Everything works fine but now i need to write to the cookie in javascript.
The problem i am having is JSON.stringify is URLEncoding the RAW value which is causing an "Invalid JSON primitive" when calling JavascriptSerializer.Deserialize on the server side. It does not know how to handle the URLEncoding.
I'd much rather get this figured out on the client side because all existing users have cookies that are NOT urlencoded.
I need a JS JSON serializer that does not URLencode!
RAW data looks like:
%7B%0A%20%20%22ClientGuid%22%3A%20%222bb1c08a-0813-4739-8f09-9e9576e6f626%22%2C%0A%20%20%22UserId%22%3A%203601%2C%0A%20%20%22OrganizationId%22%3A%20null%2C%0A%20%20%22OrganizationName%22%3A%20null%2C%0A%20%20%22CampaignId%22%3A%2087%2C%0A%20%20%22EmailAddress%22%3A%20null%2C%0A%20%20%22LastCommodityId%22%3A%20157%2C%0A%20%20%22LastRegionId%22%3A%201%2C%0A%20%20%22LastCategoryId%22%3A%205%2C%0A%20%20%22LastSpmId%22%3A%200%2C%0A%20%20%22LastSpmCategoryId%22%3A%200%2C%0A%20%20%22ShowDetailsAllCommodities%22%3A%20false%0A%7D
should look like:
{
"ClientGuid": "2bb1c08a-0813-4739-8f09-9e9576e6f626",
"UserId": 3601,
"OrganizationId": null,
"OrganizationName": null,
"CampaignId": 87,
"EmailAddress": null,
"LastCommodityId": 157,
"LastRegionId": 1,
"LastCategoryId": 5,
"LastSpmId": 0,
"LastSpmCategoryId": 0,
"ShowDetailsAllCommodities": false
}

Use decodeURIComponent on the raw data before sending it to the server:
decodeURIComponent("%7B%0A%20%20%22ClientGuid%22%3A%20%222bb1c08a-0813-4739-8f09-9e9576e6f626%22%2C%0A%20%20%22UserId%22%3A%203601%2C%0A%20%20%22OrganizationId%22%3A%20null%2C%0A%20%20%22OrganizationName%22%3A%20null%2C%0A%20%20%22CampaignId%22%3A%2087%2C%0A%20%20%22EmailAddress%22%3A%20null%2C%0A%20%20%22LastCommodityId%22%3A%20157%2C%0A%20%20%22LastRegionId%22%3A%201%2C%0A%20%20%22LastCategoryId%22%3A%205%2C%0A%20%20%22LastSpmId%22%3A%200%2C%0A%20%20%22LastSpmCategoryId%22%3A%200%2C%0A%20%20%22ShowDetailsAllCommodities%22%3A%20false%0A%7D")

Related

How to parse dynamic json reponse and get specific value and pass it as an input to next request

I get .json file as a response from an API and from that file I should parse and fins specific parameter and pass it as an input to the next request, how do I do that using Katalon.
If I say
response = JSON.parse("response.json");
it says it is unable to identify JSON as valid. Can someone help me out with the solution?
Your JSON is invalid, maybe it is a copy-paste issue.
The valid JSON should be
{
"responseStatusCode": "OK",
"data": {
"screenName": "employeeTimeslip",
"screenType": "Redirect",
"searchResultCount": 0,
"rows": [],
"tabs": [],
"searchParams": {
"employeeID": "000092926",
"timeslipNumber": "201900019701"
}
}
}
So, you were missing a "," between "OK" and "data" and two closing curly braces at the end of the file.
You can check JSON files for validity yourself using online JSON validators, for example, this one.
i found a way to read specific parameter from the json response file like below:
val scn = scenario("ClaimSubmission")
.exec(http("request_2")
.post("URL")
.headers(headers_2)
.body(RawFileBody("json file path"))
.check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))
Timeslip number would be retrieved using : .check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))

Parsing Service Bus Queue message in Logic App parameter function

I receive a error from my SharePoint Get-Item action when I try to use data from the Service Bus message triggering my Logic App (inner xml omitted):
Unable to process template language expressions in action 'Get_items' inputs at line '1' and column '1641': 'The template language function 'json' parameter is not valid. The provided value '<?xml version="1.0" encoding="utf-8"?>
<Projektaufgabe id="b92d6817-694e-e611-80ca-005056a5e651" messagename="Update">
...
</Projektaufgabe>' cannot be parsed: 'Unexpected character encountered while parsing value: . Path '', line 0, position 0.'.
The decoded message xml looks okay even quoted in the error message.
The received queue message body seems okay - only the ContentType is empty:
(ContentData truncated)
{
"ContentData": "77u/PD94bWwgdmVyc2lvbj0iMS4wIiBl...=",
"ContentType": "",
"ContentTransferEncoding": "Base64",
"Properties": {
"DeliveryCount": "1",
"EnqueuedSequenceNumber": "20000001",
"EnqueuedTimeUtc": "2016-07-29T09:03:40Z",
"ExpiresAtUtc": "2016-08-12T09:03:40Z",
"LockedUntilUtc": "2016-07-29T09:04:10Z",
"LockToken": "67796ed8-a9f0-4f6a-952b-ccf4eda00071",
"MessageId": "f3ac2ce4e7b6417386611f6817bf5da1",
"ScheduledEnqueueTimeUtc": "0001-01-01T00:00:00Z",
"SequenceNumber": "31806672388304129",
"Size": "1989",
"State": "Active",
"TimeToLive": "12096000000000"
},
"MessageId": "f3ac2ce4e7b6417386611f6817bf5da1",
"To": null,
"ReplyTo": null,
"ReplyToSessionId": null,
"Label": null,
"ScheduledEnqueueTimeUtc": "0001-01-01T00:00:00Z",
"SessionId": null,
"CorrelationId": null,
"TimeToLive": "12096000000000"
}
My parsing function for the SharePoint Get-Item OData filter looks like this:
#{json(base64ToString(triggerBody().ContentData)).Projektaufgabe.id}
I already tried to separate decoding and casting to string:
#{json(string(decodeBase64(triggerBody().ContentData))).Projektaufgabe.id}
Since it seems to be an issue decoding the message, I reckoned it wouldn't help much receiving a json message instead of xml from the Service Bus queue.
So from what I can see you are trying to convert an xml to json. You are very close - only issue is #json() expects either
A string that is a valid JSON object
An application/xml object to convert to JSON
Here, the #base64toString() is converting to a string, but you really need to let #json() know this is #2 and not #1, so changing expression to this should work:
#{json(xml(base64toBinary(triggerBody()[contentdata])))[foo]}
Let me know

FineUploader Failing to parse incoming JSON

I'm using play/scala for a webapp and a scala API, currently running simply on two different ports on localhost:9000 and localhost:8080 respectively. I have the basic page from the fine-uploader.com website docs, and a simple test page build in Play. (FWIW, i don't think much of the above is relevant)
When i post a file, chunked or not, Fine uploader receives a 200 from the API and valid JSON, but JSON.parse returns a failure. The logging from fine-uploader is below. If i take this output and run it through (in the console)
JSON.parse(JSON.stringify(json))
a valid object is returned. I can't just JSON.parse(json) directly in the browser tools b/c its already an object. I have checked the types by console.log'ing typeOf json in the qq.parseJson method and it returns string, so no conversion should be required, though i have tried it with the same results.
fine-uploader console logs:
[Fine Uploader 5.5.1] Sending simple upload request for 0 fine-uploader.js:251
[Fine Uploader 5.5.1] xhr - server response received for 0 fine-uploader.js:251
[Fine Uploader 5.5.1] responseText = {"code":"UPLOAD_COMPLETE","response":{"MediaModel":{"id":1103,"publicUri":"http://localhost:8080/media/Archive.zip","fileLocation":"/src/services/api/src/main/webapp/media/Archive.zip","mediaDate":{"year":2016,"month":2,"day":28},"mediaOrder":1,"viewName":"","caption":"","altText":"","isPublic":1,"fileSize":1107080,"created":"2016-02-28T14:58:43Z"},"UserMediaModel":{"id":1,"userId":24,"mediaId":3,"created":"2016-02-17T12:48:18Z"}},"errors":[]} fine-uploader.js:251
[Fine Uploader 5.5.1] Received response status 200 with body: {"code":"UPLOAD_COMPLETE","response":{"MediaModel":{"id":1103,"publicUri":"http://localhost:8080/media/Archive.zip","fileLocation":"/src/services/api/src/main/webapp/media/Archive.zip","mediaDate":{"year":2016,"month":2,"day":28},"mediaOrder":1,"viewName":"","caption":"","altText":"","isPublic":1,"fileSize":1107080,"created":"2016-02-28T14:58:43Z"},"UserMediaModel":{"id":1,"userId":24,"mediaId":3,"created":"2016-02-17T12:48:18Z"}},"errors":[]} fine-uploader.js:251
[Fine Uploader 5.5.1] Simple upload request failed for 0
The server is responding with a 200 per the API logs and i can see the exact response object in the Response tab of Chrome Dev tools.
I added a custom error handler, but not more more information was provided, just that the error is the text output
Error on file number 0 - Archive.zip. Reason: {"code":"UPLOAD_COMPLETE",....same as above
Lastly, all rows are propery inserted into the database as you see from the id's created above. And all logging points to an equally successful action.
Thanks for any pointers. I've also put this output in http://jsonlint.com/ and it parses fine.
Thanks! (sorry for the lack of JSON formatting, i can change it, but this seemed long enough already)
EDIT
It seems that i have satisfied the requirements stated in the other question with "success":true and Content-Type=text/plain. The following, per request is the JSON output and the headers
Headers:
Key: Access-Control-Allow-Origin Value: http://localhost:9000
Key: Date Value: Mon, 29 Feb 2016 00:55:19 GMT
Key: Access-Control-Allow-Credentials Value: true
Key: Content-Type Value: text/plain; charset=UTF-8
{
"code": "UPLOAD_COMPLETE",
"response": {
"MediaModel": {
"id": 1169,
"publicUri": "http://localhost:8080/media/Archive.zip",
"fileLocation": "/src/services/api/src/main/webapp/media/Archive.zip",
"mediaDate": {
"year": 2016,
"month": 2,
"day": 28
},
"mediaOrder": 1,
"viewName": "",
"caption": "",
"altText": "",
"isPublic": 1,
"fileSize": 1107080,
"created": "2016-02-29T00:55:19Z"
},
"UserMediaModel": {
"id": 1,
"userId": 24,
"mediaId": 3,
"created": "2016-02-17T12:48:18Z"
}
},
"errors": [],
"success": true
}
I'm sure this will end up being something silly, so i appreciate the input.
Based on the error reported by Fine Uploader, your response is not properly formatted. If you look closely at the response in your browser's network tab (such as with Chrome dev tools), you'll likely see a JSON string that, once parsed using JSON.parse results in another JSON string instead of a JavaScript object containing the expected properties. I can't confirm this as you haven't posted the exact response from your server, but I'm certain that this is the case. This is most likely caused by an encoding issue server-side. For example, your server should return this: {"foo": "bar", "success": true} instead of this: "{\"foo\": \"bar\", \"success\": true}". I suspect your server is returning the latter.

What are the differences between application/json and application/x-www-form-urlencoded?

What is the difference between
request.ContentType = "application/json; charset=utf-8";
and
webRequest.ContentType = "application/x-www-form-urlencoded";
The first case is telling the web server that you are posting JSON data as in:
{"Name": "John Smith", "Age": 23}
The second case is telling the web server that you will be encoding the parameters in the URL:
Name=John+Smith&Age=23
webRequest.ContentType = "application/x-www-form-urlencoded";
Where does application/x-www-form-urlencoded's name come from?
If you send HTTP GET request, you can use query parameters as follows:
http://example.com/path/to/page?name=ferret&color=purple
The content of the fields is encoded as a query string. The application/x-www-form-
urlencoded's name come from the previous url query parameter but the query parameters is
in where the body of request instead of url.
The whole form data is sent as a long query string.The query string contains name-
value pairs separated by & character
e.g. field1=value1&field2=value2
It can be simple request called simple - don't trigger a preflight check
Simple request must have some properties. You can look here for more info. One of
them is that there are only three values allowed for Content-Type header for simple
requests
application/x-www-form-urlencoded
multipart/form-data
text/plain
3.For mostly flat param trees, application/x-www-form-urlencoded is tried and tested.
request.ContentType = "application/json; charset=utf-8";
The data will be json format.
axios and superagent, two of the more popular npm HTTP libraries, work with JSON bodies by default.
{
"id": 1,
"name": "Foo",
"price": 123,
"tags": [
"Bar",
"Eek"
],
"stock": {
"warehouse": 300,
"retail": 20
}
}
"application/json" Content-Type is one of the Preflighted requests.
Now, if the request isn't simple request, the browser automatically sends a HTTP request before the original one by OPTIONS method to check whether it is safe to send the original request. If itis ok, Then send actual request. You can look here for more info.
application/json is beginner-friendly. URL encoded arrays can be a nightmare!
One of the biggest differences between the two is that JSON-encoding the post usually preserves the data types of the values that are sent in (as long as they are valid JSON datatypes), whereas application/x-www-form-urlencoded will usually have all properties converted to strings.
For example, in the JSON-encoded post of:
{"Name": "John Smith", "Age": 23}
the server will most likely parse the Age property as the integer 23.
Whereas in
Name=John+Smith&Age=23
the server will most likely parse Age as the string "23".
Of course, if you are using other layers to parse these values and convert them to the appropriate types, this may not be an issue.

d3.json call always gets null data

If I make this call to the server from a browser:
http://localhost:8080/api/items/number/all.json
Or from curl:
curl -G http://localhost:8080/api/items/number/all.json
I get back the following json:
{
"language": null,
"number": 10,
"queryId": 0,
"from": null,
"to": null,
"percentage": 33,
"dataInfoSet": null
}
However when I use d3.json call:
d3.json("http://localhost:8080/api/items/number/all.json", function(jsondata) {
console.log(jsondata);
});
The output from console.log is null.
If instead the http call, I save the json in a file (fileWithData.json) and do:
d3.json("fileWithData.json", function(jsondata) {
console.log(jsondata);
});
Everything works as expected. Does anyone know what might be the problem?
Solved with the help of d3-js Goole group.
The problem was that the page loading the json was not being served from localhost:8080, thus, there were cross domain restrictions. I just deployed the file within the same application.
In case cross domain calls have to be made, the group suggested the use of jasonp and specially and CORS (
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
)