【问题标题】:POST request from android to firebase cloud functions从 android 到 firebase 云函数的 POST 请求
【发布时间】:2018-04-09 22:37:44
【问题描述】:

我曾尝试分别调用我的 firebase 云函数,但除了无法访问 json 参数request.body.PARAMETER_NAME 始终返回 undefined 并卡在这一点上之外,一切似乎都运行良好。

这是我的安卓代码:

try {
                      JSONObject jsonParam = new JSONObject();
                      jsonParam.put("param1", "v1");
                      jsonParam.put("param2", 2302355);
                      jsonParam.put("param3", "v2");

                      URL url = new URL(urls[0]);
                      HttpURLConnection connection;
                      connection = (HttpURLConnection) url.openConnection();
                      connection.setRequestProperty("Authorization", idToken);
                      connection.setRequestMethod("POST");
                      connection.setRequestProperty("Content-Type", "application/json");
                      connection.setUseCaches(false);
                      connection.setAllowUserInteraction(false);
                      connection.setConnectTimeout(10000);
                      connection.setReadTimeout(10000);
                      //Write
                      OutputStream outputStream = connection.getOutputStream();
                      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                      writer.write(jsonParam.toString());
                      writer.close();
                      outputStream.close();
                      ///
                      connection.connect();
                      int res = connection.getResponseCode();
                      a = (res == HttpURLConnection.HTTP_OK);
                      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        server_response = readStream(connection.getInputStream());
                      }
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                    //return (a) ? server_response : "Nothing";
                    return server_response;

我的服务器代码:

exports.SOME_FUNCTION_NAME = functions.https.onRequest(function (request, response) {
if (!request.headers.authorization) {
    console.error('No Firebase ID token was passed');
    response.status(403).send('Unauthorized');
    return;
}
admin.auth().verifyIdToken(request.headers.authorization).then(function (decodedIdToken) {
    request.user = decodedIdToken;
    response.send(request.body + '\n'+request.params+'\n'+request.data+'\n'+request.headers);
}).catch(function (error) {
    console.error('Error while verifying Firebase ID token:', error);
    response.status(403).send('Unauthorized ' + error);
});

});

这是我不断收到的回复:

[object Object][object Object][undefined][object Object]

对于request.body.toJSON,我得到undefined作为响应

可能出了什么问题?

提前致谢。

【问题讨论】:

    标签: node.js firebase google-cloud-platform httpurlconnection google-cloud-functions


    【解决方案1】:

    我发现使用 request.body.PARAM_NAME 有效。

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 2017-12-24
      • 2019-12-21
      • 2020-03-13
      • 2020-12-01
      • 2019-03-22
      • 2020-07-06
      • 2017-10-10
      • 2021-03-11
      相关资源
      最近更新 更多