【问题标题】:Parse.Cloud.httpRequest returns 'undefined' instead of a response using POST methodParse.Cloud.httpRequest 使用 POST 方法返回“未定义”而不是响应
【发布时间】:2021-11-09 22:31:10
【问题描述】:

我正在使用 POST 方法创建 Parse.Cloud.httpRequest,以从名为“retrieveToken”的 Back4App 云代码函数中的 API 检索令牌。

Parse.Cloud.define("retrieveToken", async(request) => {
  Parse.Cloud.httpRequest({
    method: 'POST',
    url: 'https://.......',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    body: {
      "auth": {
        "identity": {
          "methods": [
            "password"
          ],
          "password": {
            "user": {
              "name": "...",
              "password": "...",
              "domain": {
                "name": "..."
              }
            }
          }
        },
        "scope": {
          "project": {
            "id": "..."
          }
        }
      }
    }
  }).then(function(httpResponse) {
    console.log('Token retrieved successfully, Status Code: ', httpResponse.status, ',\n', httpResponse.text);
  }, function(httpResponse) {
    console.error('Failed to retrieve token, retrieveToken: ', httpResponse.status, ',\n', httpResponse.text);
  });
});

如果我从 httpRequest 的主体 JSON 的“范围”中删除 "project": {"id": "..."},它会正常工作。

但是,如果我不删除那段代码,它会返回“未定义”。

我必须保留那段代码,因为只有在我指定项目 ID 时,API 才会返回一个项目级令牌。否则,我会得到一个全局级别的令牌,不能用于我的情况。

无论是否使用 Postman 中指定的项目 ID,API 都能完美运行。

我已经在这里卡了一整天了,还是想不通。我在哪里做错了,如何从 Parse.Cloud.httpRequest 获得正确的响应并指定项目 ID?

更新

我发现无论是否使用 Postman 指定的项目 ID,响应的内容长度都完全不同。

'content-length' = 6752 字节,未指定项目 ID

'content-length' = 16896 字节,指定项目 id

“未定义”是否是由响应大小限制引起的?如果是这个原因,我怎样才能得到正确的回应?我可以只接收标题吗?

【问题讨论】:

    标签: javascript parse-platform httprequest parse-cloud-code back4app


    【解决方案1】:

    部分问题的发生是因为您没有返回 http 响应。试试这个:

    Parse.Cloud.define("retrieveToken", (request) => {
      return Parse.Cloud.httpRequest({
        method: 'POST',
        url: 'https://.......',
        headers: {
          'Content-Type': 'application/json;charset=utf-8'
        },
        body: {
          "auth": {
            "identity": {
              "methods": [
                "password"
              ],
              "password": {
                "user": {
                  "name": "...",
                  "password": "...",
                  "domain": {
                    "name": "..."
                  }
                }
              }
            },
            "scope": {
              "project": {
                "id": "..."
              }
            }
          }
        }
      }).then(function(httpResponse) {
        console.log('Token retrieved successfully, Status Code: ', httpResponse.status, ',\n', httpResponse.text);
        return httpResponse;
      }, function(httpResponse) {
        console.error('Failed to retrieve token, retrieveToken: ', httpResponse.status, ',\n', httpResponse.text);
        return httpResponse;
      });
    });
    

    【讨论】:

    • 嗨,Davi,实际上我只想先使用 console.log 查看它,然后我将把令牌存储在我的数据库中。所以不需要返回httpResponse。不管怎样,谢谢你的回答XD
    【解决方案2】:

    通过向我的 Back4App 服务器添加环境变量解决了这个问题。

    NODE_OPTIONS="--max-http-header-size=16384"
    

    通过使用下面的代码,我知道了确切的问题(标题溢出):

    Parse.Cloud.define("retrieveToken", async(request) => {
      return Parse.Cloud.httpRequest({
        method: 'POST',
        url: 'https://.......',
        headers: {
          'Content-Type': 'application/json;charset=utf-8'
        },
        body: {
          "auth": {
            "identity": {
              "methods": [
                "password"
              ],
              "password": {
                "user": {
                  "name": "...",
                  "password": "...",
                  "domain": {
                    "name": "..."
                  }
                }
              }
            },
            "scope": {
              "project": {
                "id": "..."
              }
            }
          }
        }
      });
    });
    

    只有上面的代码返回了下面的错误,所有其他代码都返回了“未定义”:

    [Error]: Parse Error: Header overflow (Code: 141, Version: 1.19.2)
    

    仅供参考:我正在调用此 API

    https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 2016-11-08
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多