Search code examples
rubycurb

Ruby Curb (Curl) issue with setting parameters and cookies


I've got a series of rather large arrays of entries that I want to post into a remote Jira instance's custom fields, so I'm trying to do it with Curb under Ruby (as their API doesn't allow it, and under SQL it's a bit of a dangerous munge) I'm open to other suggestions, but I can't for the life of me work out how I can set my cookies with my initial get request, then provide the parameters and appropriate headers for the post

c = Curl::Easy.new("http://jira/secure/Dashboard.jspa")
c.verbose = true
c.http_auth_types = :basic
c.username = 'user'
c.password = 'pass'
c.perform
c.headers="X-Atlassian-Token: no-check"
params=    {:fieldConfigId=>'13499',:selectedParentOptionId=>'',:addSelectValue=>'true',:os_username=>'user',:os_password=>'pass',:addValue=>'Barry the Badger',:add=>'Add'}
url="http://jira/secure/admin/EditCustomFieldOptions!add.jspa"
c.http_post(url,params)
c.perform

It looks like it's still using the same URL I've tried using rest_client, but that seems to be misbehaving with cookies, and I do need to set the header above for the atlassian token (so it doesn't request a username/password) Has anyone got any ideas - or suggestions on what better mechanisms there might be for doing this - or better yet - what I've done wrong ;) Cheers Scott


Solution

  • Sorted it Moved everything around, and had to explicitly set enable_cookies (which is a bit nuts)

    c = Curl::Easy.new
    #set first url
    c.url = dashboard
    #c.verbose = true
    c.http_auth_types = :basic
    c.username = username
    c.password = password
    c.enable_cookies = true
    c.headers="X-Atlassian-Token: no-check"
    #perform login to first link
    c.perform
    #puts c.cookies
    #prepare url to access websudo
    c.url=websudo
    c.verbose = true
    #set password for websudo form
    params={:webSudoPassword=>password}.to_query
    #set post
    c.http_post(c.url,params)
    #prepare all variables for creating new custom field option
    params={:fieldConfigId=>cf_config:selectedParentOptionId=>'',:addSelectValue=>'true',:os_username=>username,:os_password=>password,:addValue=>cf_value,:add=>'Add'}.to_query
    c.url=addoption
    c.verbose = true
    c.http_post(c.url,params)
    

    Works a treat now, and followed the advice from Pass GET parameters with Ruby Curb and used ActiveSupport to_query