Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求 [英] Parse.com REST API Error Code 400: Bad Request from Java HTTP Request to Cloud Function

查看:138
本文介绍了Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string that I am trying to send to a Parse.com cloud function. According to the REST API documentation (https://www.parse.com/docs/rest#general-requests), it must be in json format, so I made it into a json object and converted it to a string to append to the end of the http request url.

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

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

Then I send the request as so, in my attempt to match their cURL code example as Java code:

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

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

Nonetheless, I receive a returned error code of 400 with error message "Bad Request", which I believe to be caused by unrecognizable parameters being sent to the cloud function. None of the other errors in my code trigger. Yet I verified through console logs that emailId is a normal string and the resulting JSON object, as well as its .toString() equivalent comes out as a proper string reading of a JSON object. Also this worked for another function I have in which I am creating an object in my Parse database. So why would it not work here?

以下是参考和上下文的完整功能:

Here is the full function for reference and context:

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编码.

I solved the problem by using the HttpRequest external library. It gave me better control of the request and made for easier debugging of the problem. The server was receiving the request just fine, the problem was with the JSON encoding. Rather than putting the JSON object as a parameter in the request, I inserted it into the body of the http request and encoded it in UTF-8.

最后,这是有效的代码:

In the end, this is the code that worked:

    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());

这篇关于Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆