Last active
February 2, 2018 23:57
-
-
Save kevinchisholm/51617d36050078bca7a94e7035ad50f1 to your computer and use it in GitHub Desktop.
Code Examples for Blog Post Testing HTTP POST with the Node.js request Module
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
{ | |
"name" : "easy-http-post-testing-with-the-request-module", | |
"version" : "0.0.1", | |
"description" : "Easy HTTP POST testing with the request module.", | |
"author" : "Kevin Chisholm", | |
"dependencies" : { | |
"request" : "latest", | |
"body-parser": "^1.15.2", | |
"express": "4.14.0" | |
} | |
} |
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
//require the express nodejs module | |
var express = require('express'), | |
//set an instance of exress | |
app = express(), | |
//require the body-parser nodejs module | |
bodyParser = require('body-parser'); | |
//support parsing of application/json type post data | |
app.use(bodyParser.json()); | |
//support parsing of application/x-www-form-urlencoded post data | |
app.use(bodyParser.urlencoded({ extended: true })); | |
//tell express what to do when the /about route is requested | |
app.post('/form',function (req, res) { | |
//create a json response | |
requestAsJson = JSON.stringify(req.body); | |
//set the appropriate HTTP header | |
res.setHeader('Content-Type', 'application/json'); | |
//log the output | |
console.log('The POST data received was: ' + requestAsJson); | |
//send the POST data back as JSON | |
res.end(requestAsJson); | |
}); | |
//wait for a connection | |
app.listen(5000, function () { | |
console.log('POST test server is running on port 5000'); | |
}); |
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
//set a reference to the request module | |
var request = require('request'), | |
//stubs | |
postData = {}, | |
postConfig = {}, | |
postSuccessHandler = null; | |
//create an object to send as POST data | |
postData = { | |
name:'Don Draper', | |
title:'Creative Director', | |
company: 'Sterling Cooper' | |
}; | |
//the config for our HTTP POST request | |
postConfig = { | |
url:'http://localhost:5000/form', | |
form: postData | |
}; | |
//the HTTP POST request success handler | |
postSuccessHandler = function (err, httpResponse, body) { | |
//look for this message in your JS console: | |
console.log('JSON response from the server: ' + body); | |
}; | |
//make the POST request | |
request.post(postConfig, postSuccessHandler); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment