从安卓到Firebase云函数的POST请求



我尝试多次调用我的 firebase 云函数,但一切似乎都运行良好,除了无法访问 json 参数request.body.PARAMETER_NAME总是返回未定义的参数并且卡在这一点上。

这是我的安卓代码:

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作为响应

可能出了什么问题?

提前谢谢。

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

相关内容

  • 没有找到相关文章

最新更新