【问题标题】:Making a http POST request using Arduino使用 Arduino 发出 http POST 请求
【发布时间】:2011-04-10 07:05:48
【问题描述】:

我正在尝试将信息发布到我创建和托管的 Web 项目的 API。我不确定 HTTP POST 请求的确切格式是什么。每次我尝试都会收到 HTTP 400 错误,并显示“无效动词”的消息。

示例代码:

byte server[] = {"our IP"}
..
..

client(server, 80)
..
..
client.println("POST /Api/AddParking/3");

它连接到提供的 IP 地址没有任何问题,但我得到的只是上面提到的 HTTP 错误代码 400。我不确定我是否应该在我的 POST 或 Content-Length 或之后包含 HTTP 版本任何其他信息。

【问题讨论】:

    标签: http post arduino


    【解决方案1】:

    原问题已经回答,仅供谷歌路过的人参考;这是一个更完整的示例,如何使用 Arduino 将数据发布到网络服务器:

    IPAddress server(10,0,0,138);
    String PostData = "someDataToPost";
    
    if (client.connect(server, 80)) {
      client.println("POST /Api/AddParking/3 HTTP/1.1");
      client.println("Host: 10.0.0.138");
      client.println("User-Agent: Arduino/1.0");
      client.println("Connection: close");
      client.print("Content-Length: ");
      client.println(PostData.length());
      client.println();
      client.println(PostData);
    }
    

    【讨论】:

    • client.println(PostData.length()); 缺少't'
    【解决方案2】:

    发送手工制作的 HTTP 数据包可能有点棘手,因为它们对使用的格式非常挑剔。如果您有时间,我强烈建议您阅读HTTP protocol,因为它解释了所需的语法和字段。特别是您应该查看第 5 节“请求”。

    关于您的代码,您确实需要在 POST URI 之后指定 HTTP 版本,我相信您还需要指定“Host”标头。最重要的是,您需要确保在每行的末尾有一个回车换行符 (CRLF)。因此,您的数据包应如下所示:

    POST /Api/AddParking/3 HTTP/1.1
    Host: www.yourhost.com
    

    【讨论】:

      【解决方案3】:

      另一个选项是使用 HTTPClient.h(用于 adafruit 的 ESP32 羽毛上的 arduino IDE),它可以毫不费力地处理 https。我还包含 JSON 有效负载,并且可以成功发送 IFTTT webhook。

        HTTPClient http;
        String url="https://<IPaddress>/testurl";
        String jsondata=(<properly escaped json data here>);
      
        http.begin(url); 
        http.addHeader("Content-Type", "Content-Type: application/json"); 
      
        int httpResponseCode = http.POST(jsondata); //Send the actual POST request
      
        if(httpResponseCode>0){
          String response = http.getString();  //Get the response to the request
          Serial.println(httpResponseCode);   //Print return code
          Serial.println(response);           //Print request answer
        } else {
          Serial.print("Error on sending POST: ");
          Serial.println(httpResponseCode);
      
          http.end();
      
       }
      

      【讨论】:

        【解决方案4】:

        请求也可以这样发送

         // Check if we are Connected.
         if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
            HTTPClient http;    //Declare object of class HTTPClient
        
            http.begin("http://useotools.com/");      //Specify request destination
            http.addHeader("Content-Type", "application/x-www-form-urlencoded", false, true);
            int httpCode = http.POST("type=get_desire_data&"); //Send the request
        
            Serial.println(httpCode);   //Print HTTP return code
            http.writeToStream(&Serial);  // Print the response body
        
        }
        

        【讨论】:

          猜你喜欢
          • 2017-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-17
          • 1970-01-01
          • 1970-01-01
          • 2020-07-14
          相关资源
          最近更新 更多