简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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