以下示例使用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 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调用这样的请求?
解决方法:
我认为curl命令
curl -i –upload-file /path/to/image.png –header“授权:令牌”’https:// url ….’
在Java客户端使用POST时使用PUT
资料来源:卷曲的手册页.
-T, --upload-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