How could I send POST request with data (eg. some variables) to the https server and display the results to the end user?
-
Use AJAX! (also, please provide some more details about what you're trying to do :) )Spiny Norman– Spiny Norman2010-12-16 11:34:28 +00:00Commented Dec 16, 2010 at 11:34
-
1So this node.js script is running on a server in the middle of the two?thejh– thejh2010-12-16 11:35:28 +00:00Commented 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)qqryq– qqryq2010-12-16 11:38:48 +00:00Commented 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).jwueller– jwueller2010-12-16 11:53:37 +00:00Commented 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 :$Spiny Norman– Spiny Norman2010-12-16 12:01:14 +00:00Commented Dec 16, 2010 at 12:01
Add a comment
|
1 Answer
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 ;)
8 Comments
qqryq
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.
jwueller
@qqryq: Did you read the docs? Did you call everything in the right order? "it does not work" is not a sufficient error description.
qqryq
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.
timoxley
@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?
|