0

curl -X POST \ -H "X-Parse-Application-Id: **************" \ -H "X-Parse-REST-API-Key: ****************" \ -H "Content-Type: image/jpeg" \ --data-binary '@myPicture.jpg' \ https://api.parse.com/1/files/pic.jpg

I have to upload image file into parse.com file server.i have image in both base 64 as well as binary formats but am not able request this rest service using java. can any one tell me how to excute this curl in java using HttpURLConnection. thank you in advance

Krisnela TPL
  • 37
  • 1
  • 5
  • You need to show us both a) some effort and b) a more focused question on what about `HttpURLConnection` you are unclear. – Tim Biegeleisen Aug 04 '15 at 05:06
  • i am creating an android app and for back-end i m using parse.i have learnt how to upload images using android but i want to do the same with my java web application .they have REST API to support that.they have give curl to upload images on parse but i am not able to convert that c curl into java post request. – Krisnela TPL Aug 04 '15 at 05:19
  • Please have a look here: http://stackoverflow.com/questions/29860086/convert-curl-request-into-urlconnection ... Either you or we have to dope this out and figure out what the URL and parameters are. – Tim Biegeleisen Aug 04 '15 at 05:21
  • can you please tell me how send a POST request with binary data – Krisnela TPL Aug 04 '15 at 05:27
  • OK I will post code to answer your question, but in the future please try to show us what you have already tried. – Tim Biegeleisen Aug 04 '15 at 05:29

1 Answers1

0

Here is a skeleton code against which you model your program to try to access the Parse API web service:

try {
    String uri = String.format("https://api.parse.com/");
    URL url = new URL(uri);
    // NOTE: If you already have a binary array of bytes, then
    //       just use this in place of 'postData' below
    String request        = "Some request to send";
    byte[] postData       = request.getBytes(StandardCharsets.UTF_8);

    HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
    connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
    connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    SSLContext sslContext = SSLContext.getInstance("TLS");

    // this mock TrustManager trusts EVERYTHING, which probably is NOT what you want
    // in production.  Nevertheless, to get up and running this should be fine.
    TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
        public X509Certificate[] getAcceptedIssuers() { return null; }
    };
    sslContext.init(null, new TrustManager[]{tm}, null);
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.write(postData);

    InputStream xml = connection.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(xml, writer);
    String output = writer.toString();

    System.out.println("Here is the response output: " + output);
} catch (Exception e) {
    System.out.println("Error happened when calling API parse web service.");
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360