Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP request node.js using mikeal's 'request'

I'm trying to use https://github.com/mikeal/request. What is wrong with my code? Error message is under code. In my program, I'm using my real App and Rest ID's

var request = require('request');
request.post( {
url: 'https://api.parse.com/1/classes/GameScore',
headers: {
    "X-Parse-Application-Id": "11111",
    "X-Parse-REST-API-Key": "222222",
    "Content-Type": "application/json" 
         },
body: {
    "score": 1337, "playerName": "Sean Plott", "cheatMode": false
    }
 }, 
function (error, response, body) {
  if(response.statusCode == 201){
    console.log('Status Update');
  } else {
    console.log('error: '+ response.statusCode);
    console.log(body);
  }
}
);

Error message:

node.js:134
    throw e; // process.nextTick error, or 'error' event on first tick
    ^
Error: Argument error, options.body.
at Request.init (/mnt/ws/users/$mn/mnort9/165767/node_modules/request/main.js:264:13)
at new Request (/mnt/ws/users/$mn/mnort9/165767/node_modules/request/main.js:102:8)
at request (/mnt/ws/users/$mn/mnort9/165767/node_modules/request/main.js:800:11)
at Function.post (/mnt/ws/users/$mn/mnort9/165767/node_modules/request/main.js:844:10)
at Object. (/mnt/ws/users/$mn/mnort9/165767/index.js:2:9)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
like image 540
mnort9 Avatar asked Apr 05 '12 15:04

mnort9


People also ask

How do I send a node js request?

Example code: var request = require('request') var options = { method: 'post', body: postData, // Javascript object json: true, // Use,If you are sending JSON data url: url, headers: { // Specify headers, If any } } request(options, function (err, res, body) { if (err) { console. log('Error :', err) return } console.


1 Answers

body should be a string, array or buffer. Not an object.

If you want to send json then send json

request({
    ...,
   json: { ... }
})
like image 62
Raynos Avatar answered Oct 20 '22 12:10

Raynos