admin管理员组文章数量:1323723
I have an express js server running on port 3000 and 4000 and want to send a post request from server 3000 to server 4000
I tried this:
var post_options = {
url: "http://172.28.49.9:4000/quizResponse",
timeout: 20000,
method:"POST",
encoding: "application/json; charset=utf-8",
body : {data: formdata}
};
request(post_options,
function optionalCallback(err, httpResponse, body) {
if (err) {
console.log(err);
}else
console.log(body);
});
But getting this error:
TypeError: First argument must be a string or Buffer at ClientRequest.OutgoingMessage.write (_http_outgoing.js:456:11) at Request.write (D:\restfullApi\examineerapi\node_modules\request\request.js:1514:27) at end (D:\restfullApi\examineerapi\node_modules\request\request.js:552:18) at Immediate. (D:\restfullApi\examineerapi\node_modules\request\request.js:581:7) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5) at processImmediate [as _immediateCallback] (timers.js:582:5)
This is not working as I thought. Please help me to solve this issue.
I have an express js server running on port 3000 and 4000 and want to send a post request from server 3000 to server 4000
I tried this:
var post_options = {
url: "http://172.28.49.9:4000/quizResponse",
timeout: 20000,
method:"POST",
encoding: "application/json; charset=utf-8",
body : {data: formdata}
};
request(post_options,
function optionalCallback(err, httpResponse, body) {
if (err) {
console.log(err);
}else
console.log(body);
});
But getting this error:
TypeError: First argument must be a string or Buffer at ClientRequest.OutgoingMessage.write (_http_outgoing.js:456:11) at Request.write (D:\restfullApi\examineerapi\node_modules\request\request.js:1514:27) at end (D:\restfullApi\examineerapi\node_modules\request\request.js:552:18) at Immediate. (D:\restfullApi\examineerapi\node_modules\request\request.js:581:7) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5) at processImmediate [as _immediateCallback] (timers.js:582:5)
This is not working as I thought. Please help me to solve this issue.
Share Improve this question edited Sep 1, 2017 at 10:54 Raghav Garg 3,7072 gold badges25 silver badges33 bronze badges asked Sep 1, 2017 at 9:30 abhimanyuabhimanyu 1551 gold badge3 silver badges13 bronze badges 5- 1 please add the error you are getting, upon executing the above code? – Raghav Garg Commented Sep 1, 2017 at 9:36
- TypeError: First argument must be a string or Buffer at ClientRequest.OutgoingMessage.write (_http_outgoing.js:456:11) at Request.write (D:\restfullApi\examineerapi\node_modules\request\request.js:1514:27) at end (D:\restfullApi\examineerapi\node_modules\request\request.js:552:18) at Immediate.<anonymous> (D:\restfullApi\examineerapi\node_modules\request\request.js:581:7) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5) at processImmediate [as _immediateCallback] (timers.js:582:5) – abhimanyu Commented Sep 1, 2017 at 9:38
- Possible duplicate of How to make an HTTP POST request in node.js? – Spitzbueb Commented Sep 1, 2017 at 9:39
-
for body, to be json you must set
json: true
in therequest package
. Please try with addingjson: true
in your options. – Raghav Garg Commented Sep 1, 2017 at 9:47 - Axios could also do the trick : github./mzabriskie/axios – NeoPix Commented Sep 1, 2017 at 9:50
1 Answer
Reset to default 6There is a native Node module called http
which you can use in your case. It would look like this:
const http = require('http');
var postData = JSON.stringify({user: 'sample1'});
const options = {
hostname: '172.28.49.9',
port: 4000,
path: '/quizResponse',
method: 'POST',
headers: {
'content-type': 'application/json',
'accept': 'application/json'
}
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
I suggest you read the docs: https://nodejs/api/http.html#http_http_request_options_callback
本文标签: javascriptsend post request from express js server to another express js serverStack Overflow
版权声明:本文标题:javascript - send post request from express js server to another express js server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122843a2421803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论