DaveyGravy
DaveyGravy

Reputation: 21

Ruby post request failed, getaddrinfo error

I am trying to do a SOAP POST request to eBay's web service to add an item request:

    require 'uri'
    require 'net/https'


    # Create the http object
    http = Net::HTTP.new('https://api.sandbox.ebay.com', 443)
    http.use_ssl = true
    path = '/wsapi?callname=AddItem&siteid=0&version=733&Routing=new'

    # Create the SOAP Envelope
data = <<-eot
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ebay:apis:eBLBaseComponents">
   <soapenv:Header>
      <urn:RequesterCredentials>
         <urn:eBayAuthToken>TOKEN HERE</urn:eBayAuthToken>
         <urn:Credentials>
            <urn:AppId>APP_ID</urn:AppId>
            <urn:DevId>DEV_ID</urn:DevId>
            <urn:AuthCert>AUTH_CERT</urn:AuthCert>
         </urn:Credentials>
      </urn:RequesterCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <urn:AddItemRequest>
         <urn:DetailLevel>ReturnAll</urn:DetailLevel>
         <urn:ErrorLanguage>en_US</urn:ErrorLanguage>
         <urn:Version>733</urn:Version>
      </urn:AddItemRequest>
   </soapenv:Body>
</soapenv:Envelope>
eot

# Set Headers
header = {
  'Accept-Encoding' => 'gzip,deflate',
  'Content-Type' => 'text/xml;charset=UTF-8',
  'Host' => 'api.sandbox.ebay.com',
  'Connection' => 'Keep-Alive',
  'SOAPAction' => '',
  'Content-Lenth' => '160000',
  "X-EBAY-SOA-MESSAGE-PROTOCOL" => "SOAP12", 
  "X-EBAY-SOA-SECURITY-APPNAME" => "APP_ID_HERE"}

# Post the request
resp, data_end = http.post(path, data, header)

# Output the results
puts 'Code = ' + resp.code
puts 'Message = ' + resp.body
resp.each { |key, val| puts key + ' = ' + val }
puts data_end

I had it working for a split second. Now I keep getting a getaddrinfo error whenever I run the code in IRB on Ubuntu's terminal.

I was playing around with creating sockets and I think that is how I got it to work. But when I tried to recreate the socket, I could no longer replicate the results.

Is there a better environment to launch this code in? And should I have to mess with sockets at all?

Doesn't Ruby have that sort of configuration built in with the HTTP request? If sockets are a huge part of this what sort of topics should I be researching? Is there a good resource that would show me how to setup the socket connections?

Upvotes: 0

Views: 1071

Answers (1)

SteveRawlinson
SteveRawlinson

Reputation: 915

The first argument to Net::HTTP.new is the hostname or IP address. You have provided a URI. Ruby trying to resolve "http://..." as a hostname using DNS and it's failing.

Replace that line with:

http = Net::HTTP.new('api.sandbox.ebay.com', 443)

... and it works. (Or at least it progresses past that error.)

Upvotes: 1

Related Questions