Greasemonkey: POST data causes underscores in float numbers - json

let jsonString = JSON.stringify(json);
console.log(jsonString); //prints {"5667787":"currentTaxless":99.82,"current":123.78}}
GM.xmlHttpRequest({
method: "POST",
url: "https://exampleau.tld",
data: jsonString,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
//stuff
}
});
Hi,
I am starting to feel myself stupid. I haven't found any way to feed into data regular object/array or json, no matter what I did (changed headers, added dataType, feed that json variable) - the data was not posted. Only this solution posts data. In the Greasemonkey documentation there is nothing about feeding plain json.
The problem is, that on backend, when I receive such a data - it is:
an array with single key and no value
the key is html_entity_encode(d) string
where dots in float numbers are replaced with underscores. This is what I am getting:
{"5667787":{"currentTaxless":99_82,"current":123_78}}
Qusetion: What am I doing wrong or how to post without a hassle or receive normally formatted posted data without a hassle using Greasemonkey???
Versions: Greasemonkey v4.11
Firefox v81

Nevermind.
The solution is not to
data: jsonString,,
but rather explicitly put json string under some key as value like this
data: 'data=' + jsonString,

Related

NodeJS request doesn't encode the entire form

The task is rather simple, I request the endpoint with POST request (https://banana.com/endpoint/swap.php), give it my form: { banana: ["China's Red", "Sweden's Gray"], apples: [] } and send it.
However, the Request module for NodeJS that I am using does not encode the empty array (in this case "apples") and if the endpoint doesn't receive the "apples" array, it returns an error - "Invalid JSON". I have tried doing this with already encoded strings and it works just fine. I am also unable to stringify my json and then use encodeURI(), as it will then give "bananas" and "apples" quotes around them, which will get encoded - needless to say, the endpoint doesn't like that either.
I'd really appreciate if somebody could at least point me in the right direction. As I am unsure on how to proceed with this, without creating some awful spaghetti code.
data = { banana: ["China's Red", "Sweden's Gray"], apples: [] }
result = JSON.parse(JSON.stringify(data)) .
You wouldn't get double around banana and apple and if you need to access then access it
console.log(result.banana)
console.log(result.apple)
So if you need to feed this result in post request then -
url = 'your url';
const options = {
url: url,
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8'
},
json: result
};
request.post(options, function (err, response, body) {
// do something with your data
})
Let me know if this works.

Truncating Data in Variables, cant get Full Data

i am new to nativescript angular2 programming. I want to post large json data to a server. but when i create a json format data, data becomes truncated. I think reason is the size of the holding variable.
//my code is:
let headers = new Headers({ "Content-Type": "application/json" });
let options = new RequestOptions({ headers: headers });
let body=
{
"id": "1002345",
"weekday": [{
"item1" :" data1",//string
"item2":" data2",//string
"item3":" data3",//string
"item4":" data4",//string
"item5":" data5",//string
"item6":" data6",//base64 string of an image
"item7":" data7"//base64 string of an image
}]
};
when i try to print body then data becomes truncated!!
1. Which variable can hold large data?
2. Any alternate method to post large data?
Please help me out.
//json Format:
{"id":"1002345","weekday":{"item1":"nil","item2":"nil","item3":"nil","item4":"nil","item5":0,"item6":"nil","item7":"nil"}}
//actual Json data:
{"id":"1002345","weekday":{"item1":"nil","item2":"nil","item3":"nil","item4":"nil","item5":0,"item6":"iVBORw0KGgoAAAANSUhEUgAAAlgAAAEsCAYAAAAfPc2WAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAABxpRE9UAAAAAgAAAAAAAACWAAAAKAAAAJYAAACWAAAQl/KErzoAABBjSURBVHgB7N0HrHRFFQBgioIovaggBA0gAQOKYCUYCSKCFWxgQFHsiEYg1sQaJUYUhQgCIoaggtg7KCJFBYTYEguiQAgWooKoIBb0HH0L6/PM//+7b997u/d+k5zsvrlz5875IPlPdu/OXW01jQABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIE
here item6 s some part and item7 is missing!
If by "when i try to print body" you mean you output it with console.log() you need to be aware that on most systems (including Chrome, Firefox, IE, even nodejs) the debug output from console.log() is truncated to prevent it going over a reasonable length for display.
Unless you have some other reason to believe your variable has truncated, it probably hasn't been.

Optimally send base64-encoded JSON in nodejs request

I have some base64 encoded JSON, for example: eyJiYXIiOiJibGFuIn0= (this is just {"bar": "blan"}.
I'm using axios to make an http request, although I'm willing to use another library or http if need be. I want to be able to transmit the data as a JSON string with as few steps / as efficiently as possible. Currently I do:
axios({
url,
method: "POST",
data: new Buffer(data, "base64").toString(),
headers: {
"Content-type": "application/json",
},
})
However, this reads the entire string into memory from the buffer, so I'm wondering if there is a better way. If I don't use toString it doesn't work, and the JSON parser on the server responds with: Unexpected token '.
Is there a better way to transmit data from a base64-encoded string?

.NET MVC3 HttpRequestValidation & JSON

I'm new to MVC3 framework (and .NET overall; Java veteran), so bear with me, but here goes:
Input submitted to a Controller as JSON doesn't seem to be subject to the HttpRequestValidation -- Does that sound right?
I realize if you're receiving data input via JSON you're possibly already doing more work with it, but the Controller Action doesn't seem to necessarily know whether it has JSON data at that point; input values are mapped to parameters just as they would be if they were standard POST params.
Example - I'm asynchronously submitting JSON data to my Controller like the following:
var data = { "title": $titleField.val(), "content": $textArea.val(),
"location": $location.val()
};
$.ajax(submitUrl,
{
type: "POST",
contentType: "application/json; charset=utf-8",
complete: function (data) {
//blah blah
},
dataType: 'json',
data: JSON.stringify(data)
});
}
I then receive the input in my Action:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult New(string title = "", string content = "", string location = "")
{
//yada yada
}
Doing this, params are mapped and the user can easily send tags, etc. I'm not turning ValidateInput off, and if I submit with a standard POST and remove the Stringify, it throws the error as expected. Any good reason why JSONified data would skip validation?
Edit - More specific question: If JSONified data will pass HttpRequestValidation, how can we protect against the event where someone would intentionally mock a request to send JSON data instead of post params? I haven't found a way to force the Action method to differentiate between params passed as JSON vs. those passed non-encoded.
Got an answer for my question over on asp.net - See 2nd response.
Solution involves replacing the default ModelBinder.
Any good reason why JSONified data would skip validation?
JSON is encoded => so it ensures that what transits over the wire is safe. When you use JSON.stringify all dangerous characters are encoded.

Posting JSON with JQuery

Trying to get JQuery to post JSON to a server:
$.ajax({
url: "/path/to/url",
type: "POST",
dataType: "json",
contentType: "json",
data: {"foo": "bar"},
success: function(){
alert("success :-)");
},
error: function(){
alert("fail :-(");
}
});
Problem is the data appears on the server as "foo=bar" rather than the desired "{\"foo\":\"bar\"}.
I thought specifying either the dataType or contentType params would do the trick, but no.
Anyone know the correct ajax configuration ? [or alternatively a way of serialising the 'data' parameter as JSON prior to posting ?]
Thanks!
You could use json2.js:
data: JSON.stringify({"foo": "bar"})
Datatype is for returned data. Contenttype is not applicable, see here
It can only send strings, I use JSON.stringify on my created javascript objects, in your case you could just manually code the string.
You will also need to access the string on server side, for that if you are using java I can recommened google's gson