Newbie... be gentle

I am trying to access the US Postal Service web tool for tracking. I am
new to Ruby and web programming in general so if this is a really stupid
question I apologize in advance.

According to their site the if I access the test server with:
http://testing.shippingapis.com/ShippingAPITest.dll?API=TrackV2&XML=

I should get a test response. Sure enough, if I substitute in the
correct values for userid, trackid and paste it into my browser I get
the correct “test” response. Now to do it in Ruby…

I wrote:

begin
  request_body = <<-EOT
  <TrackFieldRequest USERID="#{@user_id}">
  <TrackID ID="#{@track_id}"></TrackID>
  </TrackFieldRequest>
  EOT
end

@path = '/ShippingAPITest.dll?API=TrackV2?XML='
http_address = 'testing.shippingapis.com'
@headers = {'Content-Type' => 'text/xml'}
@http = Net::HTTP.new(http_address)

headers = @headers
headers['Content-Length'] = request_body.length.to_s

response = nil
@http.start do |http|
  response = http.request_post(@path, request_body)
end

puts "Response is:  #{response.code} => #{response.header}

#{response.body}"

and the response I get is:
Response is: 501 => #Net::HTTPNotImplemented:0x2e62754

HTTP Error 501

NOT IMPLEMENTED

The server is unable to perform the method API=TrackV2?XML= at this time.

I am sure this is something really basic, can anyone help me out?

Well I don’t know this webservice so I can’t test it totally but why do
you make it such complicated? You don’t have to assemble the http-header
yourself… little example:

#!/usr/bin/env ruby
$Verbose=true

require ‘net/http’

user_id = ‘32423423’
track_id = ‘5454545’

Net::HTTP.get_print ‘testing.shippingapis.com’,
“/ShippingAPITest.dll?API=TrackV2&XML=%3CTrackFieldRequest%20USERID=%22#{user_id}%22%3E%3CTrackID%20ID=%22#{track_id}%22%3E%3C/TrackID%3E%3C/TrackFieldRequest%3E”

Bea Martin wrote:

I am trying to access the US Postal Service web tool for tracking. I am
new to Ruby and web programming in general so if this is a really stupid
question I apologize in advance.

According to their site the if I access the test server with:
http://testing.shippingapis.com/ShippingAPITest.dll?API=TrackV2&XML=

For one thing, that’s a ‘get’ request, which means all the data you are
transmitting is tacked onto the end of the url. While this:

  response = http.request_post(@path, request_body)
end

is a ‘post’ request, which means the data is not tacked onto the end of
the url, and it is sent by other means. However, if a program on the
server is expecting the data to be attached to the end of the url,
that’s where it is going to look for it.

Howdy,

There are a couple of things going on here:

1.) First is that you seem a bit hazy on some basic HTTP concepts.

Every HTTP request has an associated method. The method determines what
the
HTTP server does with your request when you get it, and each method has
some
fairly well defined semantics specified.

The two methods that you’ll come across most are GET and POST. A GET
request is what happens when you type a URL into a browser or click on a
link. A POST is what happens when you submit a form. What you need to
be
doing here is submitting a GET request with two parameters named API and
XML. What you’re doing is submitting a POST request with the chunk of
XML
as the payload.

With a GET request, parameters are submitted on the query string after
the
question mark, and multiple parameters are submitted by putting a & in
between them. So the url: Foo.com would
have
two parameters, one named bar (with the value flux) and one named baz
(with
the value quz).

In your case the two paramaters are API (which seems to specify the API
you’re trying to use) and XML (which seems to contain a chunk of XML
with
data that API needs).

I like this online tutorial here HTTP Made Really Easy if
you’d like to know more.

2.) There are much easier ways of working with HTTP than what you’re
doing
up there (generally you don’t want to monkey around with HTTP headers if
you’re doing something simple).

Simplest possible GET I know of in Ruby is:
require ‘net/http’
Net::HTTP.get_print ‘www.example.com’, ‘/index.html’

More complicated examples can be found here:

http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Hope this helps.

MBL