When I use the Framework7.request
or app.request
method to send an AJAX REST request with a JSON payload, if one of the keys has a null
value, it is getting replaced by an empty string ''
before being sent over. This is not what I’d expect, since the null
literal value is an acceptable value in JSON (among other literal values like true
, false
, and numbers).
You may validate this issue by navigating to https://mbplautz.github.io/app/, opening the left side panel, and clicking on Send Ajax Post Request >. Make sure you are inspecting the request in your browser’s network activity debug tab (I am using chrome). Note that this request will not successfully get a response; this is not an issue – I am only concerned about the outgoing request.
This is the code that is generating the request:
$$('.ajax-request').on('click', function () {
app.request.post('/app/index.html', { isAString: 'true', isABoolean: true, isNull: null, isObject: { yes: 'itIsAnObject' } }, console.log, console.err);
});
Here is what my request looks like:
You will note the difference: the value of the isNull
key is just blank instead of null
.
I saw that there was a bug about this created on github that stated that it was resolved, so I am ensuring that I am not doing something wrong before I go to github to open another bug.
Right, it is valid JSON but you don’t send it as JSON, you send it as Form Data which is converted to something like foo=bar&var=value
and on deserialization we can’t say that in such isNull=null
you meant string or null
, so just wrap it with quotes as "null"
to be sent
This is an issue for my API, because wrapping null
in quotes makes it become treated as a string literal "null"
which is interpreted differently than the object literal null
.
It doesn’t sound like there is an option to enforce that the payload is sent as JSON instead of as Form Data, but that would be nice so I wouldn’t have to worry about it. If you can point me to where in the code the XMLHttpRequest is being made, then I’d like to see if I can update the code to allow this. (Also, if I set contentType
to application/json
, it doesn’t fix the problem either.)
Fortunately, since I am in the browser, I can just use a straight plain XMLHttpRequest as a workaround, or I supposed I could modify the API to accept a string “null” since I also own the API in this case.
If you send data as plain JSON then it will bypass null
as it is. But if you are sending it as form data then setting isNull='null'
will give you what you expect
Thank you for responding @nolimits4web. I wish to remain cordial while stating that what you are saying is incorrect. if I set isNull
to 'null'
, then when it gets to my API, it will be treated as the string "null"
and not the literal JSON value null
. These are in fact two unequivalent literals.
I have submitted issue #2751 on github with a recommendation for how to resolve this issue in the request.js source code. (By the way, your code is incredibly clear and easy to read. I am grateful for this.) In my mind, I should be able to send a request with a pure JSON payload and not be restricted to sending all raw data requests as encoded form data.
I will attempt to open a pull request to fix this issue, though this may mean I need to alter any accompanied unit tests, which may take longer. Also, this would require a change in the documentation that I would have to make.
Thank you.