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

K

Kad Kerforn

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 ??
 
A

Ammar Ali

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=3D@/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 =C2=A0(1.9.2)

----
...
attachment =3D "Filedata=3D@/Developpement/Projects/TESTS/pictures/Glen.g= if"

uri =3D
URI.parse("http://www.pivotaltracker.com/services/v3/projects/999999/stor= ies/999999/attachments")

response =3D Net::HTTP.start(uri.host, uri.port) do |http|
=C2=A0 =C2=A0 http.post(uri.path, attachment, {'X-TrackerToken' =3D> xxxx= xxx})
=C2=A0end

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

is my http.post wrong ??

http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html
 
K

Kad Kerforn

Ammar said:

Thanks a lot Amar, could not get it right ... I switched to curb (
http://github.com/taf2/curb )
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.... !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top