Skip to content

Instantly share code, notes, and snippets.

@brettg
Created October 22, 2009 18:01
Show Gist options
  • Save brettg/216139 to your computer and use it in GitHub Desktop.
Save brettg/216139 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Posterous.com API HTTP AUTH Example
require 'base64'
require 'open-uri'
require 'net/http'
user = 'email@example.com'
password = 'password'
host = 'posterous.com'
path = '/api/newpost'
# build the header ourselves
auth_header = "Basic #{Base64.encode64("#{user}:#{password}")}"
open("http://#{host}#{path}", 'Authorization' => auth_header) do |r|
puts r.read
end
# use ruby standard library in HTTP AUTH
Net::HTTP.start(host) do |http|
req = Net::HTTP::Get.new(path)
req.basic_auth user, password
response = http.request(req)
puts response.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment