【问题标题】:How to make a POST api call to google endpoints using the ardunio WiFiClient如何使用 arduino WiFiClient 对谷歌端点进行 POST api 调用
【发布时间】:2023-03-24 00:35:01
【问题描述】:

我正在使用 ESP8266 wifi 模块进行物联网项目。我已经能够使用 adafruit 提供的本教程成功地将其连接到 Internet 并向 html 页面发出 GET 请求。 但我似乎很难将它与我在谷歌开发者控制台上运行的 api 端点集成。

这是我遇到问题的代码药水

const char* Host = "www.my_app_id.appspot.com";
String PostData = "clientId=&fanId=&kueliiKey=";
String url = "/_ah/api/fan/v1/register?";
Serial.print("Requesting URL: ");
Serial.println(url);
//Connect to the client and make the api call
if (client.connect(Host, httpPort)) {
    client.println("POST " + url + " HTTP/1.1");
    client.println("Host: "+ (String)Host);
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    delay(500);
}
//Read all the lines of the reply from server and print them to Serial          
while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
}

//Serial.println();
Serial.println("closing connection");

我可以连接,但我不断收到 404 错误

lloc\umm_malloc.c
HTTP/1.1 404 Not Found
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 22 Mar 2016 12:08:06 GMT
Vary: X-Origin
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Connection: close

Not Found
closing connection;

我有什么

String url = "/_ah/api/fan/v1/register?";

可能是错误的,但我已经仔细检查并使用了 http://hurl.it 并且 api 调用有效并返回了我想要的 image of successful request 任何对此的见解将不胜感激。

谢谢。

【问题讨论】:

    标签: arduino http-headers google-cloud-endpoints iot


    【解决方案1】:

    经过大量尝试,我意识到出了什么问题,这就是我修复它的方法。

    1. 我将我的网址更改为 https 显然端点现在非常喜欢 https

      www.my_app_id.appspot.com 变成了https://my_app_id.appspot.com

    2. 然后我不得不将我的端口更改为 443,并使用 POST 标头 url 路径将 postdata 附加到调用中。

      client.println("POST" + url +" HTTP/1.1")->client.println("POST" + url + + Postdata +"HTTP/1.1");

    3. 最后我不得不从使用 WifiClient 更改为 WiFiClientSecure。

    希望这对某人有帮助:)

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多