send Content-Type: application/json post with node.js
Mikeal's request module can do this easily:
var request = require('request');var options = { uri: 'https://www.googleapis.com/urlshortener/v1/url', method: 'POST', json: { "longUrl": "http://www.google.com/" }};request(options, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body.id) // Print the shortened url. }});
Simple Example
var request = require('request');//Custom Header passvar headersOpt = { "content-type": "application/json",};request( { method:'post', url:'https://www.googleapis.com/urlshortener/v1/url', form: {name:'hello',age:25}, headers: headersOpt, json: true, }, function (error, response, body) { //Print the Response console.log(body); });
As the official documentation says:
body - entity body for PATCH, POST and PUT requests. Must be a Buffer, String or ReadStream. If json is true, then body must be a JSON-serializable object.
When sending JSON you just have to put it in body of the option.
var options = { uri: 'https://myurl.com', method: 'POST', json: true, body: {'my_date' : 'json'}}request(options, myCallback)