Created
November 12, 2011 19:19
-
-
Save alessioalex/1360979 to your computer and use it in GitHub Desktop.
request post node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}; | |
request({ | |
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) { | |
console.log(body); | |
} | |
}); |
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) {
res.jsonp(response.body);
console.log('STATUS: ' + req);
//response.getBody();
// Get the response body (JSON parsed or jQuery object for XMLs)
//console.log(response.getBody());
done();
}, function(err){
console.log(err)
})
.fail(function(res) {
console.log(res);
console.log(body);
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
You can also use Requestify as alternative..