I wanted to know, how to pass the json request in the payload, for eg: {'name' : 'test', 'value' : 'test'}
:
var post_data = {}; var post_options = { host: this._host, path: path, method: 'POST', headers: { Cookie: "session=" + session, 'Content-Type': 'application/json', 'Content-Length': post_data.length, } }; // Set up the request var post_req = http.request(post_options, function (res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('========Response========: ' + chunk); }); }); // post the data post_req.write(post_data); post_req.end();
To send the JSON with payload to the REST API endpoint, you need to enclose the JSON data in the body of the HTTP request and indicate the data type of the request body with the "Content-Type: application/json" request header.
POST requestsIn Postman, change the method next to the URL to 'POST', and under the 'Body' tab choose the 'raw' radio button and then 'JSON (application/json)' from the drop down. You can now type in the JSON you want to send along with the POST request. If this is successful, you should see the new data in your 'db.
To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).
Use the request module
npm install -S request
var request = require('request') var postData = { name: 'test', value: 'test' } var url = 'https://www.example.com' var options = { method: 'post', body: postData, json: true, url: url } request(options, function (err, res, body) { if (err) { console.error('error posting json: ', err) throw err } var headers = res.headers var statusCode = res.statusCode console.log('headers: ', headers) console.log('statusCode: ', statusCode) console.log('body: ', body) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With