Net HTTP unescapes字符?

时间:2012-02-06 19:15:32

标签: ruby httparty net-http

Ruby Net HTTP unescape字符(例如龅)?我使用带有龅的HTTParty发送请求,但请求服务器上的请求参数显示为

1 个答案:

答案 0 :(得分:3)

Net :: HTTP不进行任何转换或解码。它只是移动数据的管道。

如果您在服务器端看到转换后的字符,那么在您看到之前会有更改它们。

这显示了Net :: HTTP正在发送的内容。在test.rb中:

#!/usr/bin/env ruby
require 'ap'
require 'sinatra'

post '/' do
  t = %w[text/css text/html application/javascript]
  request.body.rewind  # in case someone already read it
  puts "request.accept             #{ request.accept             }"  # ['text/html', '*/*']
  puts "request.accept? 'text/xml' #{ request.accept? 'text/xml' }"  # true
  puts "request.preferred_type(t)  #{ request.preferred_type(t)  }"  # 'text/html'
  puts "request.body               #{ request.body               }"  # request body sent by the client (see below)
  puts "request.scheme             #{ request.scheme             }"  # "http"
  puts "request.script_name        #{ request.script_name        }"  # "/example"
  puts "request.path_info          #{ request.path_info          }"  # "/foo"
  puts "request.port               #{ request.port               }"  # 80
  puts "request.request_method     #{ request.request_method     }"  # "GET"
  puts "request.query_string       #{ request.query_string       }"  # ""
  puts "request.content_length     #{ request.content_length     }"  # length of request.body
  puts "request.media_type         #{ request.media_type         }"  # media type of request.body
  puts "request.host               #{ request.host               }"  # "example.com"
  puts "request.get?               #{ request.get?               }"  # true (similar methods for other verbs)
  puts "request.form_data?         #{ request.form_data?         }"  # false
  puts "request['SOME_HEADER']     #{ request['SOME_HEADER']     }"  # value of SOME_HEADER header
  puts "request.referrer           #{ request.referrer           }"  # the referrer of the client or '/'
  puts "request.user_agent         #{ request.user_agent         }"  # user agent (used by :agent condition)
  puts "request.cookies            #{ request.cookies            }"  # hash of browser cookies
  puts "request.xhr?               #{ request.xhr?               }"  # is this an ajax request?
  puts "request.url                #{ request.url                }"  # "http://example.com/example/foo"
  puts "request.path               #{ request.path               }"  # "/example/foo"
  puts "request.ip                 #{ request.ip                 }"  # client IP address
  puts "request.secure?            #{ request.secure?            }"  # false (would be true over ssl)
  puts "request.forwarded?         #{ request.forwarded?         }"  # true (if running behind a reverse proxy)

  ap request.env["rack.request.form_hash"]  # raw env hash handed in by Rack 
end

在test2.rb中:

#!/usr/bin/env ruby

require 'net/http'

uri = URI('http://0.0.0.0:4567/')
res = Net::HTTP.post_form(uri, 'q' => '龅')

将它们保存到相应的文件中,然后运行第一个:

ruby test.rb 
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop

其次是第二个。你应该看到类似的东西:

request.accept             ["*/*"]
request.accept? 'text/xml' text/xml
request.preferred_type(t)  text/css
request.body               #<StringIO:0x000001017187c8>
request.scheme             http
request.script_name        
request.path_info          /
request.port               4567
request.request_method     POST
request.query_string       
request.content_length     16
request.media_type         application/x-www-form-urlencoded
request.host               0.0.0.0
request.get?               false
request.form_data?         true
request['SOME_HEADER']     
request.referrer           
request.user_agent         Ruby
request.cookies            {}
request.xhr?               false
request.url                http://0.0.0.0:4567/
request.path               /
request.ip                 127.0.0.1
request.secure?            false
request.forwarded?         false
{
    "q" => "&#40837;"
}

注意最后一行。这就是Net :: HTTP作为q参数发送给POST请求的内容。