Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Arduino发出http POST请求_Http_Post_Arduino - Fatal编程技术网

使用Arduino发出http POST请求

使用Arduino发出http POST请求,http,post,arduino,Http,Post,Arduino,我正在尝试将信息发布到我创建和托管的web项目的API中。我不确定HTTP POST请求的确切格式是什么。每次尝试时,我都会收到HTTP 400错误消息,其中包含“无效动词”的消息 示例代码: byte server[] = {"our IP"} .. .. client(server, 80) .. .. client.println("POST /Api/AddParking/3"); 它连接到提供的IP地址没有任何问题,但我得到的只是上面提到的HTTP错误代码400。我不确定在我的文章

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

示例代码:

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

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

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

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

关于您的代码,您确实需要在POST URI之后指定HTTP版本,我相信您还需要指定“主机”头。除此之外,您还需要确保在每一行的末尾都有一个回车行进纸(CRLF)。因此,您的数据包应该类似于:

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

原来的问题已经回答了,但仅供通过谷歌路过的人参考;下面是一个更完整的示例,说明如何使用Arduino将数据发布到Web服务器:

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);
}

请求也可以这样发送

 // 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

}

另一种选择是使用HTTPClient.h(用于adafruit的ESP32 feather上的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();

 }
HTTPClient-http;
字符串url=”https:///testurl";
字符串jsondata=();
http.begin(url);
addHeader(“内容类型”,“内容类型:application/json”);
int-httpResponseCode=http.POST(jsondata)//发送实际的POST请求
如果(httpResponseCode>0){
String response=http.getString();//获取对请求的响应
Serial.println(httpResponseCode);//打印返回代码
Serial.println(response);//打印请求应答
}否则{
Serial.print(“发送邮件时出错:”);
Serial.println(httpResponseCode);
http.end();
}
client.println(PostData.length());缺少“t”