Search code examples
curlruby-on-rails-3.1curbnegotiatehttpi

Curl using ruby


I'm trying to use Curl with SPNEGO negotiate kind of authentication through ruby.

I tried several libraries, but seems like httpi has a way to do it through it's curb adapter like this link:

https://gist.github.com/3179054#comments

I was wondering if there's a way to send JSON data as the "data" part of my curl instead of a file as given in the link. (I mean curl -d option)

My curl goes like this:

curl -X POST -d "{"id": "12341234","fieldsRequested":["title","state","component"]}" -H >"Accept: application/json" -H "Content-Type: application/json" --negotiate -u : >https://abcd.com/find/it


Solution

  • To send JSON data with HTTPI/curb, just set your JSON string as the request body as follow:

    require 'httpi'
    require 'curb'
    require 'json'  
    # ...  
    
    req.body = {"id"=>"12341234","fieldsRequested"=>["title","state","component"]}.to_json
    
    # Then set your custom headers
    req.headers = {"Accept" => "application/json", "Content-Type" => "application/json"}
    

    Also do not enable the multipart_form_post option since multipart POST is not needed:

    req.auth.gssnegotiate
    resp = HTTPI.post req do |http|
      http.use_ssl
    end