Forum: Ruby Trouble with http post

Posted by Shaun A. (shaun_a)
on 2012-11-11 08:25
I am trying to create an http post request that sends a series of
parameters and then 512 bytes of hex data in the body.


I think the post needs to be formatted something like this:
http://url/set.cgi?dst=RES&type=11&id=1&`512 bytes here`

I can get the parameters set properly and make the call to the server:

params = {'dst' => 'RES', 'id' => '1', 'type' => "11"}
uri = URI('http://url:8081/set.cgi')
res = Net::HTTP.post_form(uri, params)


But I can't seem to add on the hex values.

Does anyone have some suggestions? It seems like I may need to go lower
level than the ruby post. It seems to want to map all the parameters so
I can't pass it a basic string.

Here's some code that works in objective-C. I'm not that familiar with
objective-C but it looks to me like the body contains the parameters and
the hex data and the ruby map function is throwing me off.

- (void)sendURL:(NSURL *)url postData:(NSData *)postData
replyType:(NSInteger)replyType target:(id)target userInfo:(id)userInfo
{
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                               cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                             timeoutInterval:REQUEST_TIMEOUT_INTERVAL];

  if (postData) {
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
  }
Posted by Hassan Schroeder (Guest)
on 2012-11-11 16:28
(Received via mailing list)
On Sat, Nov 10, 2012 at 11:25 PM, Shaun A. <lists@ruby-forum.com> wrote:
> I am trying to create an http post request that sends a series of
> parameters and then 512 bytes of hex data in the body.
>
> I think the post needs to be formatted something like this:
> http://url/set.cgi?dst=RES&type=11&id=1&`512 bytes here`

Those two statements are at odds; adding a parameter to the query
string is not putting "data in the body".

Have you read the doc for Net::HTTP? There's example code for
exactly what you're trying to do...
Posted by Shaun A. (shaun_a)
on 2012-11-11 19:09
Hi Hassan,

Thanks for the reply. I have read and re-read the NET::HTTP docs and it 
doesn't do what I want because it was showing all the parameters as 
hashes. I was trying to send three values as a hash and one as a string 
and I couldn't mash them together. I ended up figuring it out and I need 
to create the url myself rather than using any of the 'easy' methods.

This is what ended up working:

data = "dst=#{dst}&type=#{type}&id=#{id}&#{data_array.join}"
resp, data = http.post(path, data)
p resp.body
Posted by tamouse mailing lists (Guest)
on 2012-11-12 00:01
(Received via mailing list)
On Sun, Nov 11, 2012 at 12:09 PM, Shaun A. <lists@ruby-forum.com> wrote:
> data = "dst=#{dst}&type=#{type}&id=#{id}&#{data_array.join}"
> resp, data = http.post(path, data)
> p resp.body

Hi, Shaun,

I'm looking here:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/ht...
and it shows that data must be a string. What documentation are you
looking at?
Posted by Shaun A. (shaun_a)
on 2012-11-12 05:18
Hi Tamouse,

I was looking at the examples higher up on the page. The POST and POST 
with Multiple Values show the form values as hashes. I didn't make it to 
that part of the documentation. I guess I didn't read it that well after 
all!

Thanks for the link. I think I have a working solution now.

Shaun
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.