How does server handle JSON in the body of a POST - json

I am using jQuery to post JSON data to a tomcat server and the server is handling the JSON array data perfectly! It is as though I passed key=value request parameters along with the URL.
So why am I posting this? I would like to know how the server treats JSON in the body of a request and how the data ends up being interpreted as request parameters. I have Googled my a** off and all I find is how the server sends JSON back to the client.

$.ajax() converts JSON data into key-value pairs (querystring style) by default. You need to set { processData : false } in the AJAX request to keep it as raw JSON.

Related

Node.js express res.json behavior

I have been working on one of my projects using node and ng4
when I came across using res.json to send an array back to Angular 4 front end, like res.json(arrayResult). Surprisingly, I cannot use JSON.parse(arrayResult) after Angular receives it because it throws an error saying unexpected end of input. I however can access all the data through result[i], just like any normal array.
I don't quite understand why res.json() does not send my array as a string to the front end. Is there any internal conversion involved? Why I could access the content through index without even parsing it or doing any conversion with it?
The server indeed send your json data as a string. Additionally to the string, the server passes Content-Type header to your client which tells your client what kind of data you received.
So if the Content-Type was text/html your client would think that he received an HTML file.
In your case, res.json using Content-Type: application/json which tells the client that the string that he got is actually a json object, so no need for you to use JSON.parse.
You can see the Content-Type under the response headers property.

Sim808 + Arduino UNO: Get Json Object Response from Web Server

How to read JSON object response from the server?
These are the commands I use:
AT+SAPBR=3,1,"Contype","GPRS"
AT+SAPBR=3,1,"APN","internet"
AT+SAPBR=1,1
AT+SAPBR=2,1
AT+HTTPINIT
AT+HTTPSSL=1
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","https://www.mywebsite.com/getTime?token=jsdaljdaskl"
AT+HTTPPARA="REDIR",1
AT+HTTPACTION=0
The returning request should be Json object, but the Sim808 read it as html response which gives error 606.
Need to define HTTP parameter content as JSON format using the following command
AT+HTTPPARA="CONTENT","application/json"
Found this document useful HTTP Client using SIM900.

Pass JSON object vs JSON string in HTTP POST

I'm building a REST API in JAVA and C# and I was wondering about the way I should pass data to those services.
What I'm familiar with as the right way is to send JSON object as the data in the POST body:
{name:'Dor'}
but I can also pass a string and parse the JSON in my service:
'{name:'Dor'}'
What is the preferable way from performance factor? or any other factors?
Basically, if you need to send the json data across via jquery, then we need to use stringify, else the data would be serialized into to key=value pair.
So, you cannot send the json object directly via jquery ajax method.
How it works behind the hood:
In $.ajax function, if we provide data as
data :{key1:"value1", key2:"value2"}
is serialized to key1=value1&key2=value2
if we provide data as
data :'{key1:"value1", key2:"value2"}' or JSON.stringify({key1:"value1", key2:"value2"})
is sent as {key1:"value1", key2:"value2"}
So, what we can conclude is that, we cannot pass json object directly via jquery, we can send only json string. Hope this clarifies everyone.

How does JSON data get sent in an http request?

Let's say I'm making the http request below, to update some record in a mongoDB database:
PUT
http://dev.mycompany.co/ping
{"id":4432, "name":"Jane Doe", "products":[ {"id":287}, {"id":434} ] }
Notice that there is an array of two objects inside the "products" property of the JSON above. I have data that is being sent in this general format, both through Fiddler and through a ruby script I've written.
When sent through Fiddler, my data in the JSON is correctly parsed and updated into my database. When sent through my ruby script, the data in the JSON is not parsed correctly. I've been trying to figure out why this is. Now, I'm wondering how data is actually sent when a JSON is sent along with an http request. Can someone point me in the right direction?
After asking a few other questions on SO and doing a bunch more research, I've arrived at the following conclusions.
Q: How does JSON data get sent in an http request?
A: It depends on how you are sending the JSON data in the request (what is the content-type)
I've encountered two ways that JSON data is sent.
Sent using content-type application/json
With this content-type, JSON data is sent literally as-is. The literal JSON data is stored as a string and sent with the request. If your JSON is complex, with nested objects and arrays and such, this is probably what you want. For a working example of sending nested JSONs using Ruby's Net/HTTP, see the answer on this SO question I asked.
Sent using content-type x-www-form-urlencoded
This is how Ruby's Net/HTTP requests typically get sent out. The form of these requests is something like: id=343?entity=Microsoft?foo=bar. This content-type is fine until you have a complex JSON.

How to send JSON data in an Ajax request?

what would be the best way to send string (unicode text) & array-of-string data (several hundreds characters, for now formatted as a single Json object) from an HTML page to a PHP server document (to be then recorded in a db)? I'm pretty much new at Json and I'm not sure how to send a Json object server side with an Ajax call.
Right now, my idea would be to somehow serialize my Json object into a string, then send that string as a parameter to a POST Ajax call, but again, I'm not sure if this is the right way or how to serialize Json.
I don't use jquery at this time.
You can use JSON.stringify as you suggested.
JSON.stringify
But why do you want to serialize as JSON on the client side as opposed to sending as is to the server and converting to JSON on the server side? Then you could use a JSON library in whatever language your server supports.