how to translate curl -X post into java

2019-05-15 02:44发布

问题:

I am trying to translate a curl command into Java (using Apache HttpClient 4.x):

export APPLICATION_ID=SOME_ID
export REST_API_KEY=SOME_KEY

curl -i -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: image/png" \
  --data-binary @/Users/thomas/Desktop/greep-small.png \
  https://api.parse.com/1/files/greep.png

but I get the following error: {"error":"unauthorized"}.

This is what my java code looks like:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("localhost", 80, "http"); 
httpclient.getCredentialsProvider().setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()), 
        new UsernamePasswordCredentials("username", "password"));
HttpPost httpPost = new HttpPost("https://api.parse.com/1/files/greep.png");

System.out.println("executing request:\n" + httpPost.getRequestLine());

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Example-Application-Id", "SOME_ID"));
nameValuePairs.add(new BasicNameValuePair("Example-REST-API-Key", "SOME_KEY"));
nameValuePairs.add(new BasicNameValuePair("Content-Type", "image/png"));

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


HttpResponse response = httpclient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (responseEntity != null) {
    System.out.println("Response content length: " 
            + responseEntity.getContentLength());
}
System.out.println(EntityUtils.toString(responseEntity));
httpclient.getConnectionManager().shutdown();

How can I translate a curl line that starts with -H and a curl line that starts with "--data-binary"? Whats the java equivalent for -d?

  -d '{ "name":"Andrew", "picture": { "name": "greep.png", "__type": "File" } }' \

Any hint is appreciated. Thanks

回答1:

The headers don't match. The curl command is using X-Parse-Application-Id and X-Parse-REST-API-Key whereas the Java code is using Example-Application-Id and Example-REST-API-Key. I imagine you'd want those to match. Plus, you are setting them as the POST body of the request instead of as HTTP headers. You need to use one of the setHeader methods on httpPost instead. I would also recommend not explicitly setting Content-Type in such a manner. The content type is usually provided as part of the HttpEntity being posted.

To post the image content using HttpClient in Java, you would need to use a FileEntity that references the path of the file (/Users/thomas/Desktop/greep-small.png in your example). Right now you are posting the header values as name value pairs as I mentioned before.

Implementing curl -d would require doing something like passing a StringEntity to httpPost.setEntity() using the value you want to send.

Finally, the Java code is using some credentials that I don't see going on at all in the curl command.