Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created June 4, 2012 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/2870574 to your computer and use it in GitHub Desktop.
Save myronmarston/2870574 to your computer and use it in GitHub Desktop.
Demonstrating of why HTTPClient response headers are serialized as base64 strings when using psych
require 'httpclient'
client = HTTPClient.new
response = client.request(:get, "http://google.com/", nil, '', {})
puts "Headers:"
response.headers.each do |key, value|
puts "#{key} (#{key.encoding.name}): #{value} (#{value.encoding.name})"
end
➜ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]
➜ ruby use_httpclient.rb
Headers:
Location (ASCII-8BIT): http://www.google.com/ (ASCII-8BIT)
Content-Type (ASCII-8BIT): text/html; charset=UTF-8 (ASCII-8BIT)
Date (ASCII-8BIT): Mon, 04 Jun 2012 20:13:59 GMT (ASCII-8BIT)
Expires (ASCII-8BIT): Wed, 04 Jul 2012 20:13:59 GMT (ASCII-8BIT)
Cache-Control (ASCII-8BIT): public, max-age=2592000 (ASCII-8BIT)
Server (ASCII-8BIT): gws (ASCII-8BIT)
Content-Length (ASCII-8BIT): 219 (ASCII-8BIT)
X-XSS-Protection (ASCII-8BIT): 1; mode=block (ASCII-8BIT)
X-Frame-Options (ASCII-8BIT): SAMEORIGIN (ASCII-8BIT)
require 'yaml'
string = "A string"
string.force_encoding('ASCII-8BIT')
puts "YAML:"
puts string.to_yaml
puts "\nRoundtripped:"
puts YAML.load(string.to_yaml)
➜ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]
➜ ruby use_psych.rb
YAML:
--- !binary |-
QSBzdHJpbmc=
Roundtripped:
A string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment