Avatar for meaku

meaku

Member since May 2019 • Last active May 2019
  • 0 conversations
  • 1 comments

Most recent activity

  • in JavaScript
    Avatar for meaku

    Make sure to add the appropriate headers when sending JSON data:

    var data = JSON.stringify({ key: "value" });
    
    const options = {
        //...
        headers: {
          "Content-Type": "application/json",
          "Content-Length": json.length
        }
    };
    
    require("http").request(options, function(res)  {
       //...
    }).end(data);
    

    I recommend attaching an error handler :)

    const req = require("http").request(/* ... */);
    req.on("error", err => console.log("Request error: " + err.message)) ;
    req.end(data);
    
Actions