我想使用HTTPClient爲NodeJS發送2個參數的POST請求。在POST請求上空的參數
var HTTPClient = require('httpclient')
var options = {
hostname: 'localhost',
path: '/',
port: 8081,
secure: false,
method: 'POST',
headers: {
'x-powered-by': 'HTTPClient.js'
},
'Content-Type': 'application/x-www-form-urlencoded',
params:{
command:'TEST',
param1:'TEST'
}
}
var example1 = new HTTPClient(options)
example1.post('/executeGraph1', function (err, res, body) {
console.log(typeof body,body);
})
然後我用快遞趕上POST請求
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '')));
app.post('/executeGraph1', function (req, res) {
console.log("Got a POST request for grahBar");
console.log("params",req.params);
console.log("body",req.body);
console.log("query",req.query);
})
var server = app.listen(8081, function() {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
我曾嘗試在其他問題解決方案,如使用'Content-Type': 'application/x-www-form-urlencoded'
或者app.use(bodyParser.urlencoded({extended: true}));
,但evrery時間我不斷收到空變量。我曾嘗試尋找屬性身體,參數或查詢,但所有三個選項都是空陣列
有沒有人知道這裏出了什麼問題?
改變你的Content-Type的內容類型'application/x-www-form-urlencoded'到 - >'Content-Type':'application/json' – swapnesh
@swapnesh I已經試過,但結果是一樣的:( –