25 Jul
Using HTTPI with Curb to do multipart file uploads with spnego
It took a while to find a library that handled spnego requests, as opposed to shelling out to a curl --negotiate. Found httpi to work well with the curb adapter.
The problem was that the documentation didn't really explain how to do file uploads withing the httpi request block.
Wasn't too much of a big deal, but I thought it may save someone else some time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'httpi' | |
require 'curb' | |
# at some point before the request is made you'll need a kerberos ticket | |
# i.e., kinit -k -t ticket_path | |
image_path = "/tmp/some_image.jpg" | |
url = URI.escape("https://some_ssl_spnego_url.com/upload_file_here") | |
HTTPI.adapter = :curb | |
req = HTTPI::Request.new | |
req.url = url | |
# add the file to the body as a PostField | |
req.body = Curl::PostField.file("image", image_path){IO.read(image_path)} | |
# use spnego when in prod and devserver | |
req.auth.gssnegotiate | |
resp = HTTPI.post req do |http| | |
http.use_ssl | |
http.multipart_form_post = true | |
end | |
if resp.code == 200 | |
puts "Successfully uploaded image." | |
else | |
puts "Error .... :(" | |
end |