How to convert curl call with “-i --upload-file” into java Unirest or any other http request?

前端 未结 3 1099
孤城傲影
孤城傲影 2020-12-18 10:46

The example below uses cURL to upload image file included as a binary file.

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


        
3条回答
  •  無奈伤痛
    2020-12-18 11:25

    After several hours of banging my head against the wall, I finally figured out how to convert the curl call into a RestClient one (I'm using Ruby on Rails).

    I think the problem you're having is that you have to pass the MIME type as the Content-Type in the request headers.

    I'm using MiniMagick to figure out the MIME type of the image I'm uploading to LinkedIn. MiniMagick can also give you the binary string of the image that LinkedIn requires, so it's a win-win situation.

    This is the call that finally worked:

    file = MiniMagick::Image.open(FILE_PATH)
    RestClient.post(UPLOAD_URL, file.to_blob, { 'Authorization': 'Bearer TOKEN', 'Content-Type': file.mime_type })
    

提交回复
热议问题