Firebase AT命令HTTP POST

时间:2017-06-20 09:27:00

标签: firebase-realtime-database arduino http-post at-command gprs

我正在尝试使用Firebase数据库中的GPRS模块发帖。调制解调器的AT命令由Arduino Uno发送。

GPRScommand("AT+CIPSEND", ">", "yy", 10000, 1);
delay(500);
GPRSboard.print("POST /.json?auth=");
GPRSboard.print(FIREBASE_AUTH);
GPRSboard.print(" HTTP/1.1");
GPRSboard.print("\r\n");
GPRSboard.print("Host: ");
GPRSboard.print("XXXXXXXXXXXXXXX-461a8.firebaseio.com");
GPRSboard.print("\r\n");
GPRSboard.print("Content-Type: ");
GPRSboard.print("application/json");
GPRSboard.print("\r\n");
GPRSboard.print("Content-Length: 16");
GPRSboard.print("\r\n");   
GPRSboard.print("Cache-Control: ");
GPRSboard.print("no-cache");
GPRSboard.print("\r\n");
GPRSboard.print("\r\n");
GPRSboard.print("{\"param1\":\"7\"}");
GPRScommand(end_c, "HTTP/1.1", "yy", 30000, 1); 

它让我回复:

+CIPRCV:384,HTTP/1.1 400 Bad Request 

我尝试过使用Postman。当我写出要在" Body TAB"中发布的数据时。邮差的作品。我用JSON格式写道:

{
"param1":"7"
}

但我认为我需要x-www-form-urlencoded或文本格式的代码我真的不知道。

如果我看到"代码"由Postman在成功的帖子中生成我有这个:

POST /.json?auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1
Host: XXXXXXXXXXXXXXX-461a8.firebaseio.com
Content-Type: application/json
Cache-Control: no-cache Postman-Token: 2f95151e-52e1-28e7-9583-c0a8f50f77d7
{ "param1":"7" }

这是我试图发送到GPRS模块的,我收到了HTTP Error 400错误请求,我在开始时已经说过了。

有人可以帮助我吗?

非常感谢

2 个答案:

答案 0 :(得分:0)

不是解决方案,而是旁听评论。您可以使用此表示法或类似方法使代码更小,更易读。

[编辑:]这有助于我解析您的请求并发现ajax是16,而它应该是14。

Content-Length

答案 1 :(得分:0)

我也面临同样的问题。

我设法通过处理发布:我已经用通用文本替换了所有值。

PostRequest post = new PostRequest("https://XXXXXX.firebaseio.com/message_list.json");
  post.addJson("{\"Key\":" + 100 + "}");
  post.addHeader("Content-Type", "application/json");
  post.addHeader("Authorization", "key=XXXXXX");
  post.send();
  System.out.println("Reponse Content: " + post.getContent());
  System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));

我试图通过AT命令在我的arduino上创建一个POST请求:

char* request =  "POST / HTTP/1.0\r\nHost: XXXXXX.firebaseio.com/.json?auth=XXXXXX\r\nContent-Type: application/json\r\nContent-Length: XX\r\n\r\n{\"Key\":\"value\"}\r\n";

也采用以下格式:

char* request =  "POST / HTTP/1.0\r\nHost: https://XXXXXX/message_list.json\r\nAuthorization: key=XXXXXX\r\nContent-Type: application/json\r\nContent-Length: 13\r\n\r\n{\"hello\":\"2\"}"; 

也尝试了没有内容

char* request =  "POST / HTTP/1.1\r\nHost: https://airwatch-yuki.firebaseio.com/message_list.json\r\nAuthorization: key=AIzaSyAgYd0MIblEmo4elyxAYpAdgXhNTweVqhw\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n";

哪些帖子

POST / HTTP/1.0
Host: https://XXXXXX.firebaseio.com/message_list.json
Authorization: key=XXXXXX
Content-Type: application/json
Content-Length: 13

{"hello":"2"}

但是得到以下错误:

+IPD,179:HTTP/1.0 400 Bad Request
Content-Length: 54
Content-Type: text/html; charset=UTF-8
Date: Thu, 23 Nov 2017 18:08:48 GMT

<html><title>Error 400 (Bad Request)!!1</title></html>CLOSED

当我在处理中创建代码时,我最初有这个Bad请求。但是当我使用.addjson函数作为键值时解决了它。我不确定char请求的区别。