ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何将带有“-i -upload-file”的curl调用转换为java Unirest或任何其他http请求?

2019-10-01 18:59:55  阅读:314  来源: 互联网

标签:unirest java curl api linkedin


以下示例使用cURL上载作为二进制文件包含的图像文件.

curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....' 

它工作正常.我需要从我的Java应用程序发出这个请求.

我试过下一个代码

URL image_url = Thread.currentThread().getContextClassLoader().getResource("jobs_image.jpg");
String path = image_url.getFile();
HttpResponse<String> response = Unirest.post(uploadUrl)
  .header("cache-control", "no-cache")
  .header("X-Restli-Protocol-Version", "2.0.0")
  .header("Authorization", "Bearer " + token + "")
  .field("file", new File(path))
  .asString();

但是,它返回状态400 Bad Request.
有没有办法从Java调用这样的请求?

这是来自LinkedIn v2 API的请求:
https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context#upload-image-binary-file

解决方法:

我认为curl命令
curl -i –upload-file /path/to/image.png –header“授权:令牌”’https:// url ….’

在Java客户端使用POST时使用PUT

资料来源:卷曲的手册页.

       -T, --upload-file <file>
              This  transfers  the  specified local file to the remote URL. If
              there is no file part in the specified URL, Curl will append the
              local file name. NOTE that you must use a trailing / on the last
              directory to really prove to Curl that there is no file name  or
              curl will think that your last directory name is the remote file
              name to use. That will most likely cause the upload operation to
              fail. If this is used on an HTTP(S) server, the PUT command will
              be used.

不确定这是否是实际问题.您的API文档链接实际上指定了POST.

标签:unirest,java,curl,api,linkedin
来源: https://codeday.me/bug/20191001/1839703.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有