I need to post some data in Rails to an external ASP.
My curl command is working:
printf 'foo=bar\r\nparameter1=4711\r\nparameter2=4712' | curl --data-binary @- http://example.com/request.asp
How do I do this with net/http?
Its important to keep the ‘\r\n’ between the parameters.
My coding so far:
uri = URI.parse('http://example.com/request.asp')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data(params)
response = http.request(req)
How do I set up params?
I tried something like this:
params = {
'foo' => 'bar\r\n',
'parameter1' => '4711\r\n',
'parameter2' => '4712'
}
But its not working. I need to encode it but how?
I tried it with the URL encoded version of ‘\r\n’: %0D%0A but it didn’t work. Is there any way to send it as binary like curl does?
Thanks.
Edit: Thanks to your answers I found the solution:
uri = URI.parse('http://example.com/request.asp')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(url.request_uri)
req.body = "foo=bar\r\nparameter1=4711\r\nparameter2=4712"
response = http.request(req)
set_form_data a) encodes and b) adds “&” both nothing I needed. Setting the body directly was the solution.
If it’s ok that
\r\nand all but the first=gets URL encoded in the POST data you could try:What is the main problem, get rid of the pair separator
&or that\r\nshould be sent unencoded? or both?