如何使用ardunio WiFiClient对谷歌端点进行POST api调用

时间:2016-03-22 21:19:55

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

我正在使用ESP8266 wifi模块进行iot项目。我已经能够成功地将它连接到互联网,并使用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并且api调用有效且返回我想要的image of successful request任何有关此内容的见解将会非常感激。

感谢。

1 个答案:

答案 0 :(得分:2)

经过大量的尝试,我意识到出了什么问题,以及我如何解决它。

  1. 我将我的网址改为https,显然端点非常支持https吧

    www.my_app_id.appspot.com成为https://my_app_id.appspot.com;

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

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

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

  4. 希望这有助于某人:)