I have a cURL that I am trying to translate into Ruby.
the cURL is this:
curl -i -k -H "Accept: application/json" -H "Authorization: token" -H "Content-Type: image/jpeg" -H "Content-Length: 44062" --data-binary "gullfoss.jpg" http://www.someurl.com/objects
My Ruby code is this:
image = File.read('uploads/images/gullfoss.jpg')
result = RestClient.post('http://www.someurl.com/objects',image,{'upload' => image, 'Accept' => 'application/json', 'Authorization' => 'token', 'Content-Type' => 'image/jpeg', 'Content-Length' => '44062'})
The Ruby code is giving me back a 400 Bad Request. It's not a problem with Authorization or the other headers. I think the problem lies with translating --data-binary to Ruby.
Any help appreciated.
Thanks, Adrian
There are a few things which might cause the problem.
File
object withRestClient
, so useFile.open
; it returns a file object.RestClient.post
, which takes argumentsurl, payload, headers={}
, your request should take one of the two following forms:Is the endpoint expecting named parameters? If so then use the following:
Technique 1: the payload includes a field or parameter named 'upload' and the value is set to the image object
Is the endpoint expecting a simple file upload? If so then use:
Technique 2: the image file object is sent as the sole payload
Note that I didn't include the
'Content-Length'
header; this is not usually required when usingRestClient
because it inserts it for you when processing files.There is another way to send payloads with
RestClient
explained here.Make sure to read through the official docs on github as well.
I am also new to
RestClient
so if I'm wrong in this case don't be too harsh; I will correct any misconceptions of mine immediately. Hope this helps!