3

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

3
  • In the example I have I am setting the post data to the image but I have also tried leaving that blank. Commented May 21, 2012 at 14:25
  • Try image = File.open('uploads/images/gullfoss.jpg','rb') {|io| io.read} Commented May 21, 2012 at 15:10
  • stackoverflow.com/questions/2863164/… Commented May 21, 2012 at 15:14

1 Answer 1

7

There are a few things which might cause the problem.

  1. You want to use an actual File object with RestClient, so use File.open; it returns a file object.
  2. You are including the image object twice in your request; the actual content is not made clear from your question. You are mixing payload elements with headers, for instance. Based on the signature of RestClient.post, which takes arguments url, 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

result = RestClient.post(
  'http://www.someurl.com/objects',
  { 'upload' => image },
  'Accept' => 'application/json',
  'Authorization' => 'token',
  'Content-Type' => 'image/jpeg',
)

Is the endpoint expecting a simple file upload? If so then use:

Technique 2: the image file object is sent as the sole payload

result = RestClient.post(
  'http://www.someurl.com/objects',
  image,
  'Accept' => 'application/json',
  'Authorization' => 'token',
  'Content-Type' => 'image/jpeg',
)

Note that I didn't include the 'Content-Length' header; this is not usually required when using RestClient 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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.