0

Trying to get data from a POST obj, should be simple enough. Using the REQUEST module https://github.com/request/request

Should be super simple, missing something though as all I return is an empty object in my console.

request.post('http://jsonplaceholder.typicode.com/posts/1', function(error, response, body){
    if(!error && response.statusCode == 200){
      console.log(body);
    }
});
diwao11
  • 161
  • 2
  • 15

1 Answers1

0

The link you're trying to request doesn't support POST requests, try with GET:

request.get('http://jsonplaceholder.typicode.com/posts/1', function(error, response, body){
    if(!error && response.statusCode == 200){
      console.log(body);
    }
});

You're confusing the http request type (GET/POST) and the url you're requesting. You can read more about it here. GET is for reading data (that's what you're doing) and POST is for sending data to the server (when creating new user for example).

You can use another route to make it less confusing (http://jsonplaceholder.typicode.com/albums for example).

Community
  • 1
  • 1
Shanoor
  • 11,466
  • 2
  • 23
  • 37
  • I get what you're saying with the GET and the POST, but can't I use a POST request to hit that API and return the data? – diwao11 Jan 27 '16 at 06:12
  • @diwao11 No, POST requests are used to *send* data not read them. You have an [example](https://github.com/typicode/jsonplaceholder#creating-a-resource), this creates a new post/user/album and returns the created object. – Shanoor Jan 27 '16 at 06:40