如何翻译curl -X post到java [英] how to translate curl -X post into java

查看:1860
本文介绍了如何翻译curl -X post到java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将curl命令翻译成Java(使用Apache HttpClient 4.x):

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

但我得到以下错误:{error:未授权}。

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

这是我的java代码看起来像这样:

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

如何翻译以-H开头的卷曲行和以 - 开头的卷曲行? -data-binary?

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" } }' \

任何提示是赞赏。感谢

推荐答案

标题不匹配。 curl 命令使用 X-Parse-Application-Id X-Parse-REST -API-Key ,而Java代码使用示例应用程序Id 示例REST- / code>。我想你会希望那些匹配。此外,您将其设置为请求的 POST 正文,而不是HTTP标头。您需要使用 httpPost 的%20java.lang.String%29rel =nofollow> setHeader 方法。我还建议不要以这种方式显式设置 Content-Type 。内容类型通常作为 HttpEntity 的一部分提供。

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.

要使用HttpClient发布图像内容Java,您需要使用 /Users/thomas/Desktop/greep-small.png)的title =FileEntity> FileEntity )。

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.

实现 curl -d 会将标题值作为名称值对发布。需要执行类似传递 StringEntity httpPost.setEntity()使用您要发送的值。

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

最后,Java代码使用了一些在 curl 命令中没有看到的凭据。

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

这篇关于如何翻译curl -X post到java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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