使用 Arduino 发出 http POST 请求 [英] Making a http POST request using Arduino

查看:184
本文介绍了使用 Arduino 发出 http POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to post information to an API on a web project that I have created and hosted. I am not sure what the exact format is for the HTTP POST request. Every time I try I get HTTP 400 errors with the message that there is "an invalid verb".

示例代码:

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

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

它连接到提供的 IP 地址没有任何问题,但我在上面提到的 HTTP 错误代码 400 中返回.我不确定是否应该在我的 POST 或 Content-Length 或任何其他信息.

It connects to the IP address supplied without any problems, but all I get back in the above mentioned HTTP error code 400. I am not sure if I was supposed to include a HTTP version after my POST or and Content-Length or any other information.

推荐答案

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

The original question is already answered, but just for reference for people passing by via Google; here is a more complete example how to post data to a webserver with an 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);
}

这篇关于使用 Arduino 发出 http POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆