Ad

What Is The Difference Between JSON And Data In Node.js Request?

- 1 answer

I want to know about the difference between json and data in node HTTP requests

var http = require('http');

let api = {
    url: "https://someurl/api/ticket/create",
    method: "POST",
    headers:
    {        
        "cache-control": "no-cache",
        "Content-Type": "application/json",
        "Accept": "application/json"
    },
    data:{

    }
}

let apiTwo = {
    url: "https://someurl/api/ticket/create",
    method: "POST",
    headers:
    {        
        "cache-control": "no-cache",
        "Content-Type": "application/json",
        "Accept": "application/json"
    },
    json:{

    }
}


http.request(api,function(err,resp,body){ // 400 for api and 200 for apiTwo 

    if(err){
        console.log(err);
    }
    else{
        console.log(resp);
        console.log(body);
    }
})

When I hit the above API with data as key in the request, I get 400. When I hit the same API with json as key in request I am getting 200.

In short, my question is this json and data are configured on the server? What is the difference between them? Which one is preferred when?

Ad

Answer

I got to know that after searching a lot, APIs are handled on the server-side for data and json separately. So when you pass json or data in your API request, go through the documentation of APIs for the right choice between json or data.

Ad
source: stackoverflow.com
Ad