【问题标题】:Arduino Http Post with JSON带有 JSON 的 Arduino Http Post
【发布时间】:2014-05-08 15:37:38
【问题描述】:

我正在尝试通过 Arduino 发布 json 数据。当我尝试使用此代码时。我将使用 QueryString 发送 json 数据。如果我尝试此代码,服务器会以错误的 QueryString 格式回答我。这意味着我已连接到服务器,服务器获取了我的数据。

 if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("POST /URL?query=jsondata HTTP/1.1");
client.println("Host: **.**.**.**");
client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);

}

但我的主要计划是使用查询字符串发送我的 json 数据。如果我尝试此代码;

client.println("POST /URL?query={request:{Header:{Username:kullaniciAdi,Password:123456},Item:{Serial:ABC123QWE,Data:100, DateOn:23/11/1986 15:45:24}}} HTTP/1.1");

我收到 HTTP 错误 400。请求格式错误。

有人有什么想法吗?

【问题讨论】:

    标签: json post arduino http-post


    【解决方案1】:

    是的,您的 URI 包含空格,并且可能包含其他字符以混淆发布请求的格式。您需要对这些字符进行编码。

    据我所知,Arduino 标准库不包含任何形式的 urlEncode 方法,这在其他语言和库中很常见,因此您必须自己创建或寻找一个。

    您生成的代码将类似于:

    String request = "/URL?query={request:{Header:{Username:kullaniciAdi,Password:123456},Item:{Serial:ABC123QWE,Data:100, DateOn:23/11/1986 15:45:24}}}";
    String encRequest = uriEncode(request); // need to write your own method for this...
    String post = "POST " + encRequest + " HTTP/1.1");
    client.println( post);
    

    关于创建 uriEncode 函数的一些讨论在 Arduino Forum 上,似乎还有一个 working method on hardwarefun.com

    【讨论】:

    • 我必须包含使用 Var 和 encodeURIComponent 的任何库吗?
    • 抱歉,我认为这是一个 javascript 问题,我正在修改我的答案以给出更以 Arduino 为中心的响应
    猜你喜欢
    • 2016-03-12
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多