5

I have to make a request to an API with XML:

http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxxxxxxx"> <ZipCode ID= "0"> <Zip5>90210</Zip5> </ZipCode> </CityStateLookupRequest>

I'm trying to use Nokogiri to achieve this, but I don't know how to add the USERID="xxxx.." part. This is what I have (incomplete):

def xml_for_initial_request
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.CityStateLookupRequest.USERIDhowdoIsetthevalue?? {
      xml.Zip {
        xml.Zip5 '90210'
      }
    }
  end
end
0

1 Answer 1

10

I would do as below :

require 'nokogiri'

builder = Nokogiri::XML::Builder.new do |xml|
    xml.CityStateLookupRequest('userid' => 'xxxxxx' ) {
      xml.zip("id" => '10'){
        xml.Zip5 '90210'
      }
    }
end

puts builder.to_xml
# >> <?xml version="1.0"?>
# >> <CityStateLookupRequest userid="xxxxxx">
# >>   <zip id="10">
# >>     <Zip5>90210</Zip5>
# >>   </zip>
# >> </CityStateLookupRequest>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.