undefined method strip for HTTP::Message

时间:2018-03-06 13:01:57

标签: ruby

I want to rewrite this code using http client:

http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Post.new(url)
    request.basic_auth(user, pass)
    request['content-type'] = 'application/xml'
    request['cache-control'] = 'no-cache'
    request.body = request_body

    response = http.request(request).body
    response = Response.new(response)
    check_for_approved_response(response)

Into this code:

request = HTTPClient.new()
request.set_auth(url, user, pass)

response = request.post(url, request_body)
response = Response.new(response)
check_for_approved_response(response)

But I get this result when I run the code:

undefined method `strip' for #<HTTP::Message:0x007fdbbf3cfdc8>
/Users/user/.rvm/gems/ruby-2.2.2/gems/nori-2.6.0/lib/nori.rb:43:in `parse'

at this line:

Nori.new(parser: :nokogiri, advanced_typecasting: false, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }).parse(raw_response)

I tried to require:

require 'httpclient'
require 'active_support/all'
require 'active_support/core_ext'
require 'active_support/core_ext/object'

But I still get this error. Do you know how I can fix this issue?

1 个答案:

答案 0 :(得分:0)

看起来Nori期望字符串不是HTTP :: Message对象。您应该直接传递raw_response.body而不是raw_response对象吗?

Nori.new(parser: :nokogiri, advanced_typecasting: false, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }).parse(raw_response.body)