P
P
Peter2014-11-03 16:29:11
Ruby on Rails
Peter, 2014-11-03 16:29:11

How to send an http post file with parameters using Ruby?

The request should be similar to the one below:

curl -X POST -H "Content-Type:application/octet-stream" "http://example.com/api/v1/data?api_key=API_KEY" --data-binary "@json_file.json"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily, 2014-11-05
@friflex

require 'net/http'

uri = URI('http://example.com/api/v1/data?api_key=API_KEY')
req = Net::HTTP::Post.new(uri)
req.body = File.read('json_file.json')
req.content_type = 'application/octet-stream'

res = Net::HTTP.start(uri.hostname, uri.port) {|http|
  http.request(req)
}

See the net/http documentation for details .

V
Viktor Vsk, 2014-11-03
@viktorvsk

stackoverflow.com/questions/184178/ruby-how-to-pos...
or

`curl -X POST -H "Content-Type:application/octet-stream" "http://example.com/api/v1/data?api_key=API_KEY" --data-binary "@json_file.json"`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question