如何使用 arduino WiFiClient 对 google 端点进行 POST api 调用

How to make a POST api call to google endpoints using the ardunio WiFiClient

我正在使用 ESP8266 wifi 模块开发物联网项目。我已经能够成功地将它连接到互联网并使用 adafruit 提供的本教程向 html 页面发出 GET 请求。 但我似乎很难将它与我在 google 开发人员控制台上 运行 的 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 and the api call works and returns what I want image of successful request 任何对此的见解将不胜感激。

谢谢。

经过大量尝试后,我意识到出了什么问题,下面是我如何修复它的。

  1. 我将 url 更改为 https 显然端点现在非常支持 https

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

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

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

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

希望这对某人有所帮助:)