How to rewrite a curl request into a NET::HTTP one?

I have a curl request (to upload a file) which is running weel in the
console

$curl -H “X-TrackerToken: xxxxxxx” -X POST -F
Filedata=@/Developpement/Projects/TESTS/pictures/Glen.gif
http://www.pivotaltracker.com/services/v3/projects/999999/stories/999999/attachments

I tried to mimic it in my ruby script (1.9.2)



attachment = “Filedata=@/Developpement/Projects/TESTS/pictures/Glen.gif”

uri =
URI.parse(“http://www.pivotaltracker.com/services/v3/projects/999999/stories/999999/attachments”)

response = Net::HTTP.start(uri.host, uri.port) do |http|
http.post(uri.path, attachment, {‘X-TrackerToken’ => xxxxxxx})
end

but this doesn’t work and gave me an Internal Server Error

is my http.post wrong ??

On Sat, Aug 28, 2010 at 4:56 PM, Kad K. [email protected]
wrote:

is my http.post wrong ??

Ammar A. wrote:

On Sat, Aug 28, 2010 at 4:56 PM, Kad K. [email protected]
wrote:

is my http.post wrong ??

Stanislav Vitvitskiy's blog: Multipart post in Ruby

Thanks a lot Amar, could not get it right … I switched to curb (
GitHub - taf2/curb: Ruby bindings for libcurl )
and it works ( 5 mn to install and understand it then running …)

def add_attachment(story_id, filepath)

resource_uri = 

URI.parse(“http://#{@base_url}/#{@project_id}/stories/#{story_id}/attachments”)

request = Curl::Easy.new("#{resource_uri}") do |curl|
  curl.headers["X-TrackerToken"] = @token
  curl.on_success do |response|
      doc = Hpricot(response.body_str).at('attachment')
      @return = { :id  => doc.at('id').innerHTML.to_i, :status   => 

doc.at(‘status’).innerHTML }
end
curl.on_failure do |response, err|
begin
raise TrackerException.new(err.inspect)
rescue TrackerException => e
logger = Log4r::Logger[‘test’]
logger.error(“ADD ATTACHMENT : #{e.message}”)
logger.error(“#{e.backtrace}”)
@return = nil
end
end
end
request.multipart_form_post = true
request.http_post(Curl::PostField.file(“Filedata”, “#{filepath}”))
@return
end

no worry about building a multi-part form… !