Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to POST JSON data #271

Closed
EvanOman opened this issue Jun 15, 2012 · 5 comments · May be fixed by niklabh/request#10, nyteshade/request#11, grnd/request#11, enterstudio/request#6 or gjvis/request#7
Closed

How to POST JSON data #271

EvanOman opened this issue Jun 15, 2012 · 5 comments · May be fixed by niklabh/request#10, nyteshade/request#11, grnd/request#11, enterstudio/request#6 or gjvis/request#7

Comments

@EvanOman
Copy link

Hello, I am having issues posting data with your Request module. I am trying to POST JSON data to a locally running Bugzilla server. Using PHP curl works no problem but when I use the following code:

var request = require('request');
request.post('url', 
{
    json: 
    {
        email: "email", 
        hash: "hash"
    }
}
);

I get no data being passed on(we checked it with a script). what is the proper format for a post? On this support page I have seen people use the json map to be a True/False value and use form or body to hold the data but then I have seen even more people place the data straight into the json map. Which is the proper method? The documentation is very vague on the options so I am very confused. Help would be greatly appreciated, thanks!

@mikeal
Copy link
Member

mikeal commented Jun 15, 2012

that's how you post JSON. using true is mainly for GET (it adds proper accept headers). we use this feature to post JSON in production so i'm quite confused that it wouldn't be working.

there's even a test for it in test-body which is passing.

@EvanOman
Copy link
Author

Alright maybe I'm using something else wrong then, here is my actual request:

var request = require('request');
request('http://software-pc/PhpProject4/test_file.php',{
    method: 'POST', 
    json: {
        method: "Bugzilla.time"
    }
},
function(error, response, data) {
    console.log(data);
});

And here is my PHP test:

<?php
 if ($_REQUEST['method']!= NULL)
 {
     echo 'Got the data';
 }
 else{
     echo "Didnt work\n";
     var_dump($_REQUEST);
 }
?>

Which returns:

Didn't Work
Array(0)  {
}

in the console. Anything stick out that I'm doing wrong? I really appreciate the input, thanks!

@Tivoli
Copy link

Tivoli commented Jun 28, 2012

Echo the request body, perhaps it's not being parsed as JSON on the PHP side and you're simply getting the JSON string instead of an object with the method field.

@mikeal
Copy link
Member

mikeal commented Jun 28, 2012

var request = require('request')
  , http = require('http')
  ;
http.createServer(function (req, res) {
  console.log(req.headers)
  req.on('data', function (d) {console.log(d.toString())})
  req.on('end', function () {res.writeHead(200); res.end();})
}).listen(8080, function () {
  request('http://localhost:8080',  
    { method: 'POST' 
    , json: { method: "Bugzilla.time"}
    },
  function(error, response, data) {
    console.log(data)
  }
  )
})
{ host: 'localhost:8080',
  'content-type': 'application/json',
  accept: 'application/json',
  'content-length': '26',
  connection: 'keep-alive' }
{"method":"Bugzilla.time"}
undefined

seems to work fine

@Tivoli
Copy link

Tivoli commented Jun 28, 2012

Was directing that to @EvanOman since it may not be parsing the string to JSON on his PHP server :P. So he might want to check what's in $_REQUEST.

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