how to translate curl -X post into java

怎甘沉沦 提交于 2019-12-06 02:57:00

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!