4

How could I send POST request with data (eg. some variables) to the https server and display the results to the end user?

5
  • Use AJAX! (also, please provide some more details about what you're trying to do :) ) Commented Dec 16, 2010 at 11:34
  • 1
    So this node.js script is running on a server in the middle of the two? Commented Dec 16, 2010 at 11:35
  • It is implementation of some rest API, and for authentication I need to ask API server for permission token, I need to use POST request with some secret ID so it is not possible to use AJAX (it has to be done on server side without any client integration) Commented Dec 16, 2010 at 11:38
  • @Spiny Norman: node.js does not know what AJAX is. It is a client-side concept. AJAX requests are handled like every other request(server-side). Commented Dec 16, 2010 at 11:53
  • Ah, right. Please disregard my comment: I didn't know about node.js and was assuming it was just some javascript file :$ Commented Dec 16, 2010 at 12:01

1 Answer 1

4

Use the http.Client module. From the docs:

var http = require('http');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/', {'host': 'www.google.com'});
request.end();
request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

I am pretty sure that you can exchange GET with POST ;)

Sign up to request clarification or add additional context in comments.

8 Comments

ok, and how to send the data here? I know that I have to set 'security' arg to 'true' in createClient() for https, but I have no idea how to send data.
@qqryq: Use request.write. request is a http.ClientRequest object. The docs tell you everything you need.
@qqryq: Did you read the docs? Did you call everything in the right order? "it does not work" is not a sufficient error description.
yes, i read the DOCs before I create that question in here, maybe I just didn't understand something. I use request.write after that google.request() and before request.end but the server I'm connecting to behaves like there were no data in the request.
@qqryq "i fixed it, but I'm not going to post how I fixed it" = fuuuuuuuu. Can you please post your corrected code/explain what was wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.