Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 来自ESP8266的Firestore REST API POST请求 我正试图从ESP8266在CloudFireStore中保存一条信息。_Json_Google Cloud Firestore_Arduino_Iot_Arduino Esp8266 - Fatal编程技术网

Json 来自ESP8266的Firestore REST API POST请求 我正试图从ESP8266在CloudFireStore中保存一条信息。

Json 来自ESP8266的Firestore REST API POST请求 我正试图从ESP8266在CloudFireStore中保存一条信息。,json,google-cloud-firestore,arduino,iot,arduino-esp8266,Json,Google Cloud Firestore,Arduino,Iot,Arduino Esp8266,为此,我修改了一个示例代码 我尝试了GET请求,请求成功并返回了以前存储的文档。但是,当我尝试POST请求时,出现以下错误: { "error": { "code": 400, "message": "Document parent name \"projects/dummy-1cb41/databases/(default)/documents/users\" lacks \"

为此,我修改了一个示例代码

我尝试了GET请求,请求成功并返回了以前存储的文档。但是,当我尝试POST请求时,出现以下错误:

{
  "error": {
    "code": 400,
    "message": "Document parent name \"projects/dummy-1cb41/databases/(default)/documents/users\" lacks \"/\" at index 56.",
    "status": "INVALID_ARGUMENT"
  }
}

我认为我没有正确地实现json有效负载主体。我有很多尝试,但都做不好

请帮忙! 这是密码

/**
   BasicHTTPSClient.ino

    Created on: 20.08.2018

*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};

ESP8266WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("ssid", "password");
}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

//    client->setFingerprint(fingerprint);

    client->setInsecure();

    HTTPClient https;

    String url = "https://firestore.googleapis.com/v1beta1/projects/dummy-1cb41/databases/(default)/documents/users/doc-1";

    Serial.print("[HTTPS] begin...\n");
    if (https.begin(*client, url)) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
//      int httpCode = https.GET();
      https.addHeader("Content-Type", "application/json");
 
      int httpCode = https.POST("{\"fields\":{\"name\":{\"stringValue\":\"a name\"}}}");
      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        } else {
          Serial.println(https.getString());
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }

  Serial.println("Wait 10s before next round...");
  delay(10000);
}

/**
BasicHTTPSClient.ino
创建日期:2018年8月20日
*/
#包括
#包括
#包括
#包括
#包括
//演示URL的指纹将于2021年6月2日到期,需要在此日期之前进行更新
常量指纹[20]={0x40,0xaf,0x00,0x6b,0xec,0x90,0x22,0x41,0x8e,0xa3,0xad,0xfa,0x1a,0xe8,0x25,0x41,0x1d,0x1a,0x54,0xb3};
ESP8266WiFiMulti WiFiMulti;
无效设置(){
序列号开始(115200);
//Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
对于(uint8_t=4;t>0;t--){
Serial.printf(“[设置]等待%d..\n”,t);
Serial.flush();
延迟(1000);
}
WiFi.模式(WiFi_STA);
WiFiMulti.addAP(“ssid”、“密码”);
}
void循环(){
//等待WiFi连接
如果((WiFiMulti.run()==WL_已连接)){
std::unique_ptrclient(新BearSSL::WiFiClientSecure);
//客户端->设置指纹(指纹);
客户端->设置不安全();
HTTPClient-https;
字符串url=”https://firestore.googleapis.com/v1beta1/projects/dummy-1cb41/databases/(默认)/文档/用户/doc-1”;
Serial.print(“[HTTPS]开始…\n”);
如果(https.begin(*client,url)){//https
Serial.print(“[HTTPS]GET…\n”);
//启动连接并发送HTTP头
//int-httpCode=https.GET();
https.addHeader(“内容类型”、“应用程序/json”);
int-httpCode=https.POST(“{\”字段\“:{\”名称\“:{\”字符串值\“:\”名称\“}”);
//httpCode在出现错误时为负值
如果(httpCode>0){
//HTTP标头已发送,服务器响应标头已处理
Serial.printf(“[HTTPS]获取…代码:%d\n”,httpCode);
//在服务器上找到文件
如果(httpCode==HTTP_代码_确定| | httpCode==HTTP_代码_永久移动){
字符串有效负载=https.getString();
Serial.println(有效载荷);
}否则{
Serial.println(https.getString());
}
}否则{
Serial.printf(“[HTTPS]获取…失败,错误:%s\n”,HTTPS.errorToString(httpCode.c_str());
}
https.end();
}否则{
Serial.printf(“[HTTPS]无法连接\n”);
}
}
Serial.println(“在下一轮之前等待10秒…”);
延迟(10 000);
}

我已经解决了这个问题。感谢路易斯·曼努埃尔和本·T.的评论和提示


问题是,我犯了一个愚蠢的错误,使用了相同的GET请求的URL作为POST请求。这意味着我试图在Firestore中的文档中创建一个文档,但无法在文档中创建文档。新文档必须在集合中创建。因此,在通过删除末尾的doc-1来更正URL之后,新的URL将如下…/users/。它工作得很好

所以,不久前我遇到了一个问题,我记得我通过测试gcp的api解决了这个问题,但是错误告诉我们,当发送请求时,字符串中缺少一个字符“/”,我会检查文档和字符串以检查缺少的内容。您是否发布到与get相同的URL?如果是这样,您应该使用Firebase API使用put或补丁来更新资源。是的!事实上,我发现问题出在URL中,因为我试图在一个文档中添加一个文档,这肯定会失败。我刚刚更正了URL并删除了URL末尾的/doc-1,现在它可以正常工作了。谢谢你们的评论