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

查看:36
本文介绍了如何将 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":"unauthorized"}.

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"开头的卷曲线?-d 的 java 等价物是什么?

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

推荐答案

标题不匹配.curl 命令使用 X-Parse-Application-IdX-Parse-REST-API-Key 而Java 代码使用 Example-Application-IdExample-REST-API-Key.我想你会希望那些匹配.另外,您将它们设置为请求的 POST 正文而不是 HTTP 标头.您需要使用 setHeader 方法代替 httpPost.我还建议不要以这种方式显式设置 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.

要在 Java 中使用 HttpClient 发布图像内容,您需要使用 FileEntity 引用文件路径 (/Users/thomas/Desktop/greep-small.png 在您的示例中).正如我之前提到的,现在您将标题值作为名称值对发布.

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 需要做一些事情,比如传递一个 StringEntityhttpPost.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天全站免登陆