开发者

Convert to string/text ruby

开发者 https://www.devze.com 2023-01-19 07:43 出处:网络
I\'m using the following code to download a page through a POST request: require \'net/http\' require \'uri\'

I'm using the following code to download a page through a POST request:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'开发者_如何学JAVApost'=>'1'})
puts res.split("Date")

The URL I originally used has been replaced with example.com

It works great, but when I try to call split (last line) it returns an error:

<main>': undefined methodsplit' for # (NoMethodError)

I'm new to ruby, so I'm confused about this error.


The method you are calling returns a HTTPResponse object, so you need to leverage that object's methods to get what you want. maybe something like:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.body.split("Date")

Notice the body method.

Or, if you want to see all the data returned:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.inspect

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消