繁体   English   中英

Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求

[英]Parse.com REST API Error Code 400: Bad Request from Java HTTP Request to Cloud Function

我有一个要发送到Parse.com云函数的字符串。 根据REST API文档( https://www.parse.com/docs/rest#general-requests ),它必须为json格式,因此我将其制成json对象并将其转换为字符串以附加到http请求网址的末尾。

    JSONObject jsonParam = new JSONObject();
    jsonParam.put("emailId", emailId);
    String urlParameters = jsonParam.toString();

然后我以这种方式发送请求,以尝试将其cURL代码示例匹配为Java代码:

        con.setDoOutput(true);
        DataOutputStream wr = null;
        wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

尽管如此,我仍然收到返回的错误代码400,并带有错误消息“ Bad Request”,我认为这是由于无法识别的参数被发送到云功能所致。 我的代码触发器中没有其他错误。 但是我通过控制台日志验证了emailId是正常字符串,并且生成的JSON对象及其等效的.toString()是作为JSON对象的正确字符串读取而出现的。 同样,这对于我在Parse数据库中创建对象的另一个功能也起作用。 那么,为什么在这里不起作用?

这是供参考和上下文使用的完整功能:

private void sendEmailWithParse(String emailId) throws IOException {
    String url = "https://api.parse.com/1/functions/sendEmailNow";
    URL obj = new URL(url);
    HttpsURLConnection con = null;
    try {
        con = (HttpsURLConnection) obj.openConnection();
    } catch (IOException e) {
        System.out.println("Failed to connect to http link");
        e.printStackTrace();
    }

    //add request header
    try {
        con.setRequestMethod("POST");
    } catch (ProtocolException e) {
        System.out.println("Failed to set to POST");
        e.printStackTrace();
    }
    con.setRequestProperty("X-Parse-Application-Id", "**************************************");
    con.setRequestProperty("X-Parse-REST-API-Key", "************************************************");
    con.setRequestProperty("Content-Type", "application/json");

    JSONObject jsonParam = new JSONObject();
    jsonParam.put("emailId", emailId);
    System.out.println("parameter being sent to cloud function: " + jsonParam);
    System.out.println("parameter being sent to cloud function as string: " + jsonParam.toString());
    String urlParameters = jsonParam.toString();

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = null;
    try {
        wr = new DataOutputStream(con.getOutputStream());
    } catch (IOException e1) {
        System.out.println("Failed to get output stream");
        e1.printStackTrace();
    }
    try {
        wr.writeBytes(urlParameters);
    } catch (IOException e) {
        System.out.println("Failed to connect to send over Parse object as parameter");
        e.printStackTrace();
    }
    try {
        wr.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        wr.close();
    } catch (IOException e) {
        System.out.println("Failed to connect to close datastream connection");
        e.printStackTrace();
    }

    int responseCode = 0;
    try {
        responseCode = con.getResponseCode();
    } catch (IOException e) {
        System.out.println("Failed to connect to get response code");
        e.printStackTrace();
    }
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);
    System.out.println("Response message: " + con.getResponseMessage());
}

我通过使用HttpRequest外部库解决了该问题。 它使我可以更好地控制请求,并使调试问题更容易。 服务器正好收到请求,问题出在JSON编码。 我没有将JSON对象作为请求中的参数,而是将其插入到http请求的主体中并以UTF-8编码。

最后,这是有效的代码:

    String url = "https://api.parse.com/1/functions/sendEmailNow";
    URL obj = new URL(url);

    //Attempt to use HttpRequest to send post request to parse cloud
    HttpRequest request = HttpRequest.post(obj).contentType("application/json");
    request.header("X-Parse-Application-Id", "**************************");
    request.header("X-Parse-REST-API-Key", "********************");
    JSONObject jsonParam = new JSONObject();
    jsonParam.put("emailId", emailId);
    request.send(jsonParam.toString().getBytes("UTF8"));

    if (request.ok())
        System.out.println("HttpRequest WORKED");
    else
        System.out.println("HttpRequest FAILED " + request.code() + request.body());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM