Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby and net/http request without content-type

I'm trying to make a call to a Tika server using Net::HTTP::Put. The issue is that the call always passes the Content-Type, which keeps Tika from running the detectors (which I want) and then chokes due to the default Content-Type of application/x-www-form-urlencoded. Tika docs suggest to not use that.

So, I have the following:

require 'net/http'
port = 9998
host = "localhost"
path = "/meta"

req = Net::HTTP::Put.new(path)
req.body_stream = File.open(file_name)
req['Transfer-Encoding'] = 'chunked'
req['Accept'] = 'application/json'
response = Net::HTTP.new(host, port).start { |http|
    http.request(req)
}

I tried adding req.delete('content-type') and setting initheaders = {} in various ways, but the default content-type keeps getting sent.

Any insights would be greatly appreciated, since I would rather avoid having to make multiple curl calls ... is there any way to suppress the sending of that default header?

like image 844
opennomad Avatar asked Jan 24 '26 22:01

opennomad


1 Answers

If you set req['Content-Type'] = nil then Net::HTTP will set it to the default of 'application/x-www-form-urlencoded', but if you set it to a blank string Net::HTTP leaves it alone:

req['Content-Type'] = ''

Tika should see that as an invalid type and enable the detectors.

like image 151
infused Avatar answered Jan 26 '26 16:01

infused