【问题标题】:Firestore REST API POST request from ESP8266来自 ESP8266 的 Firestore REST API POST 请求
【发布时间】:2020-06-15 17:17:14
【问题描述】:

我正在尝试从 ESP8266 将一条信息保存在 Cloud Firestore 中。

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

我尝试了 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);
}

【问题讨论】:

  • 所以,前段时间我遇到了一个问题,我记得我通过 gcp 的 Test this api link 解决了这个问题,但是错误告诉我们的是你缺少一个发送请求时字符串中的字符“/”,我会检查文档与字符串以检查缺少的内容。
  • 您发布到与获取相同的 URL 吗?如果是这样,您应该使用 Firebase API 来更新资源。
  • 是的!实际上我发现问题出在 URL 中,因为我试图在一个肯定会失败的文档中添加一个文档。我刚刚更正了 URL 并删除了 URL 末尾的 /doc-1,现在它按预期工作了。谢谢你们的cmets

标签: json google-cloud-firestore arduino iot arduino-esp8266


【解决方案1】:

我已经解决了这个问题。感谢 Luis Manuel 和 Ben T 的 cmets 和提示。

问题是我犯了一个愚蠢的错误,将 GET 请求的相同 URL 用于 POST 请求。这意味着我试图在 Firestore 中的文档内创建一个文档,而这是不可能在文档内创建文档的。新文档必须在集合内创建。因此,在通过删除最后的 doc-1 来更正 URL 后,新的 URL 将是这样的 .../users/。它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2018-08-04
    • 2020-04-06
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多