Node.js 使用NodeJS发送post请求

Node.js 使用NodeJS发送post请求,node.js,Node.js,我正在尝试向pi上安装的服务发送post请求。以下是我正在使用的应用程序: const http = require('http') const data = JSON.stringify({ Name:"MYONE", }) const options = { hostname: 'http://127.0.0.1', port: 11555, PATH:'/api/config', method: 'POST', headers: { 'Conten

我正在尝试向pi上安装的服务发送post请求。以下是我正在使用的应用程序:

const http = require('http')

const data = JSON.stringify({
    Name:"MYONE",

})

const options = {
  hostname: 'http://127.0.0.1',
  port: 11555,
  PATH:'/api/config',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }


}

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', (d) => {
    process.stdout.write(d)
  })
})

req.on('error', (error) => {
  console.error(error)
})

req.write(data)
req.end()
应用程序应该更改设备的名称。根据文档,如果“一切正常”,返回状态应该为200 运行应用程序时,我会得到以下信息:

node app.js 




statusCode: 200
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Changing device name </title>
<link rel="stylesheet" type="text/css" href="/style.css" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<frameset rows="52,20,16,*" frameborder="0" framespacing="0" border="0">
  <frameset cols="*,771,*" frameborder="0" framespacing="0" border="0">
    <frame src="/menu?logo=b" noresize scrolling="no" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
    <frame name="toplogo" src="/menu?logo=t" noresize scrolling="no" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
    <frame src="/white.html" noresize scrolling="no" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
  </frameset>
  <frame name="navibar" src="/menu" noresize scrolling="no" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
  <frame name="submenu" src="/grey.html" noresize scrolling="no" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
  <frame name="content" src="/menu?info=Browser" marginheight="0" marginwidth="0" framespacing="0" frameborder="0" border="0">
<noframes>You must use a browser with frame support to see this page.</noframes>
</frameset>
</html>
node app.js
状态代码:200
更改设备名称
必须使用具有框架支持的浏览器才能查看此页面。
最后我得到了状态码200,但设备名称没有改变。 你知道我错过了什么吗?
我希望我能提供更多关于这项服务的信息,但我没有任何信息。

请求看起来没问题。您可以发布
/api/config
端点吗?看起来,在收到POST请求后,它会用一个HTML页面进行响应,并且应该在该页面上更改名称。“你确定你没有遗漏什么吗?”塞巴斯蒂安卡兹马雷克感谢你的回答。我肯定我错过了smt。我一直在寻找/api/config,但找不到它:(也许你需要重新启动设备来应用这些更改?