Node.js请求的行为与Postman和Curl不同

时间:2018-11-02 17:30:29

标签: node.js curl request postman

我已经把它弄乱了一段时间,似乎无法弄清楚……我正试图按顺序向设备发送POST请求(我猜什么设备都没关系)更改它的IP地址。通常,设置是通过浏览器完成的,因此我尝试模拟该请求。当我用Postman或Curl而不是Node.js的请求模块发出请求时,它似乎运行良好。 所有请求均返回200 OK状态代码,但似乎不接受后者。这是我使用邮递员执行的请求:

POST /configuration_tab/ajax_comb_table_save/network_config/network_config_schema HTTP/1.1
Host: 192.168.1.250
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: PHPSESSID=mg37okvhkr1eqv1okq99i98i62
Cache-Control: no-cache
Postman-Token: 234ece69-c3f8-2d29-34a5-c40f8e64f708

form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26
IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26
IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26
SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable

这是我通过卷曲发送的:

curl -X POST "http://192.168.1.250/configuration_tab/ajax_comb_table_save/network_config/network_config_schema" 
-H "Content-Type:application/x-www-form-urlencoded; charset=UTF-8" 
-H "Cookie:PHPSESSID=mg37okvhkr1eqv1okq99i98i62" 
-d "form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable"

同样,它们两个都工作正常。现在,这是我用来通过Node尝试相同操作的代码:

let body = "form=Hostname%3D%26IPv4mode%3Dstatic%26IPv4address%3D192.168.1.250%26IPv4netmask%3D255.255.255.0%26IPv4gateway%3D0.0.0.0%26DNSmode%3Dmanual%26IPv4DNS1%3D%26IPv4DNS2%3D%26IpAddrCnflctDetectEnbl%3Denable%26NtpServer%3D%26SyslogServer%3D%26SyslogPort%3D514%26SshServerEnable%3Denable"

request({
    url: "http://192.168.1.250/configuration_tab/ajax_comb_table_save/network_config/network_config_schema",
    method: "POST",
    headers: {
        "Cookie": "PHPSESSID=mg37okvhkr1eqv1okq99i98i62",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    body: body
}, (err, resp) => {
    // Do whatever...
});

有人知道什么可能与我有所不同吗?任何帮助将不胜感激。

0 个答案:

没有答案