# Newbie... be gentle

**URL:** <https://www.ruby-forum.com/t/newbie-be-gentle/116588>\
**Category:** Ruby\
**Created:** [October 7, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/newbie-be-gentle/116588 "2007-10-07T17:56:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bamm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bamm](https://www.ruby-forum.com/u/bamm)\
**Post date:** [October 7, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/newbie-be-gentle/116588/1 "2007-10-07T17:56:52Z")

</div>

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=](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?

---

<div class="post-metadata">

**Author:** ![bamm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bamm](https://www.ruby-forum.com/u/bamm)\
**Post date:** [October 7, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/newbie-be-gentle/116588/2 "2007-10-07T18:40:44Z")

</div>

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](http://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”

---

<div class="post-metadata">

**Author:** ![bamm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bamm](https://www.ruby-forum.com/u/bamm)\
**Post date:** [October 7, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/newbie-be-gentle/116588/3 "2007-10-07T18:50:32Z")

</div>

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=](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.

---

<div class="post-metadata">

**Author:** ![bamm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bamm](https://www.ruby-forum.com/u/bamm)\
**Post date:** [October 8, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/newbie-be-gentle/116588/4 "2007-10-08T23:03:02Z")

</div>

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](http://www.foo.com?bar=flux&baz=quz) 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](http://www.jmarshall.com/easy/http/) 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](http://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](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html)

Hope this helps.

MBL
