node.js - Webduino 从 curl 而不是从 Node 接收 POST 参数

标签 node.js http curl arduino

这是在 Arduino 上运行的代码,使用 Webduino图书馆:

void handleConfigRequest(WebServer &server, WebServer::ConnectionType type, char *, bool) {

// POST request to receive new config
if (type == WebServer::POST) {

  bool repeat;
  char name[16], value[50];

  do {
      // Returns false when no more params to read from the input
      repeat = server.readPOSTparam(name, 16, value, 50);

      if (strcmp(name, "dName") == 0) {
        strcpy(deviceName, value);
        Serial.println(value);
      }
  } while (repeat);
}

当在命令行上从 curl 执行以下命令时,这按预期工作(并通过串行打印“测试”):

curl http://10.0.1.141/config -d "dName=Test"

我还测试了一个简单的 HTML 表单,它也可以在提交时通过串行打印“测试”:

<form action="http://10.0.1.141/config">
  <input type="text" name="dName" value="test">
  <input type="submit" value="Send">
</form>

但是,以下 Node.js 使用 request代码不起作用:

var options = {
    url: "http://10.0.1.141/config",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Accept": "*/*",
      "User-Agent": "TestingScript"
    },
    form: {dName: "Test"}
  }

  request.post(options, function(error, response, body) {
    console.log(error, response, body)
  })

使用 Node 代码,Arduino 确认 HTTP POST 请求(在本例中,通过闪烁 LED),但不会将“测试”打印到串行。

我已将 curl 和我的 Node 代码指向 requestb.in 页面,您可以看到请求本身看起来是相同的(底部的是 curl,顶部是我的):

Requestbin

有什么建议吗?

最佳答案

最后,我将 Node request 代码替换为以下 jQuery:

$.ajax({
  type: "POST",
  url: "http://10.0.1.141/config",
  data: {dName: "Test"}
}, function(data( {
  console.log(data)
}

效果很好。奇怪的。

关于node.js - Webduino 从 curl 而不是从 Node 接收 POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43873339/

相关文章:

javascript - 我如何将这些功能与 promise 联系在一起?

node.js - 捕获node.js中未捕获异常的源或行号

javascript - AJAX:仅触发错误回调

shell - 使用cURL在网页中登录

javascript - 当我尝试提交 HTML 表单时显示错误 "Cannot POST"

windows - 通过安装程序 (MSI) Windows 7 更新 Node.js 时看不到最新版本

http - 如何找出数据包的 HTTP header 长度?

http - 注册后重定向到使用用户凭据登录?

vba - 尝试在我的 MS-Access 数据库程序中集成 HTTP GET 请求

php - 在 Windows 8 上使用 PHP 5.6 启用 CURL