HTTP Posts in Ruby
Sometimes I need to make simple HTTP posts in a Ruby script. Using a 3rd party library may be too clunky for such a simple script.
This post helps a lot, but it doesn't cover all my needs. Specifically, when working with the Switchvox API, the API methods are expected to be a JSON parameter.
Let's start by imply posting some JSON.
Post some JSON
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("http://localhost:3000/users")
header = {'Content-Type': 'text/json'}
user = {user: {
name: 'Bob',
email: 'bob@example.com'
}
}
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = user.to_json
# Send the request
response = http.request(request)
This will simply submit a POST request with the JSON as the body.
But, there were some methods that required a file be submitted with the JSON? Things get a little more complicated when you do this, but it is still manageable.
Post a file with some JSON
require 'net/http'
require 'uri'
require 'json'
require 'mime/types'
uri = URI.parse("http://localhost:3000/users")
BOUNDARY = "AaB03x"
header = {"Content-Type": "multipart/form-data, boundary=#{BOUNDARY}"}
user = {user: {
name: 'Bob',
email: 'bob@example.com'
}
}
file = "test_file.xml"
# We're going to compile all the parts of the body into an array, then join them into one single string
# This method reads the given file into memory all at once, thus it might not work well for large files
post_body = []
# Add the file Data
post_body << "--#{BOUNDARY}\r\n"
post_body << "Content-Disposition: form-data; name=\"user[][image]\"; filename=\"#{File.bsename(file)}\"\r\n"
post_body << "Content-Type: #{MIME::Types.type_for(file)}\r\n\r\n"
post_body << File.read(file)
# Add the JSON
post_body << "--#{BOUNDARY}\r\n"
post_body << "Content-Disposition: form-data; name=\"user[]\"\r\n\r\n"
post_body << user.to_json
post_body << "\r\n\r\n--#{BOUNDARY}--\r\n"
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = post_body.join
# Send the request
response = http.request(request)
And there you have it! A multipart HTTP Post in Ruby without an external library. You can probably see how you could add multiple files and sets JSON objects.
Is it worth not using a 3rd party library and doing it yourself?
Written by Daniel Westendorf
Related protips
9 Responses
Great post! I've finally got it working thanks to you. Cheers!
Hi,
I am trying to send a request to a REST API. It involves 2 types of data:
1. XML
2. .doc file
I followed your instructions. All went well. but in response I got following message:
{"Message":"Expected MIME multipart request."}
Please, guide
For those of you who, like me, are pounding your head on the table for the past 2 days, be advised:
in the line
header = {"Content-Type": "multipart/form-data, boundary=#{BOUNDARY}"}
after form-data there's a comma [,], but it should be a semicolon[;]
Anyway, thanks for the (otherwise excelent) post
after hours of trying httparty etc this solution save me.Bravo
By using a 3rd library you dont have to deal with encoding, cookies, redirections.... among other things. If you want simplicity but power and flexibility I strongly recommend you the nice_http gem https://github.com/MarioRuiz/nicehttp
great native solution!
Useful! Thanks!
Interesting
Wow! Amazing post