Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
request post node.js
var request = require('request'), default_headers, site_root = 'http://localhost:3000';;
default_headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
// 'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
url: site_root + '/login',
headers: default_headers,
method: 'POST',
body: JSON.stringify({ user:'my_user', password:'my_pass' })
}, function (err, res, body) {
if (!err && res.statusCode == 200) {

This comment has been minimized.

Copy link

@ranm8 ranm8 commented Aug 3, 2013

You can also use Requestify as alternative..

var requestify = require('requestify');'', {
    hello: 'world'
.then(function(response) {
    // Get the response body (JSON parsed or jQuery object for XMLs)

This comment has been minimized.

Copy link

@ashabhatt ashabhatt commented Sep 9, 2014

Hi Ranm8 ,

I am doing as below code

describe('contact service test', function () {
it('should return 200 for a GET request', function () {
var requestify = require('requestify');

    var req = requestify.get('http://localhost:3001/api/contacts/54499096')
        .then(function (response) {
            console.log('STATUS: ' + req);
            // Get the response body (JSON parsed or jQuery object for XMLs)
        }, function(err){
        .fail(function(res) {
            console.log('STATUS: ' + res.statuscode);


Here I want to display the data which comes in body.. but its not working. Can you please suggest me that how can i get the body data in cmd.

and if I add wrong id in api then it should display error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment