urllib2 - My POST Request just isn't working right

Gregory Piñero gregpinero at gmail.com
Wed Feb 1 10:53:47 EST 2006


Hi guys,

I'm trying to write a program to get FedEx shipping rates, and I'm
following their API as outlined in this document:
http://www.fedex.com/us/solutions/wis/pdf/fsm_directmanual.pdf

I need specifically to make a POST request to their server as shown on
page 12.  However I continually get server  error 500, and they FedEx
support advised me that this means my request is incorrect somewhere.

I put the code I"m using at the end of this email.  Unfortunately I
don't know how to "see" the request the code generates either so I
can't paste that here.

Any help would be greatly appriciated.

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

Code pasted below (contains sample info so it won't work for you):

import urllib
import urllib2

FedEx_API_URL="https://gatewaybeta.fedex.com:443/GatewayDC"

xml_request="""<?xml version="1.0" encoding="UTF-8" ?>
<FDXSubscriptionRequest xmlns:api="http://www.fedex.com/fsmapi"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:noNamespaceSchemaLocation="FDXSubscriptionRequest.xsd">
<RequestHeader>
<CustomerTransactionIdentifier>String</CustomerTransactionIdentifier>
<AccountNumber>123456789</AccountNumber>
</RequestHeader>
<Contact>
<PersonName>Jim Smith</PersonName>
<CompanyName>Creative Widgets</CompanyName>
<Department>Shipping</Department>
<PhoneNumber>5405559900</PhoneNumber>
<PagerNumber>9999999999</PagerNumber>
<FaxNumber>5405559901</FaxNumber>
<E-MailAddress>jim at abc.com</E-MailAddress>
</Contact>
<Address>
<Line1>123 Main Street</Line1>
<Line2>1st Floor</Line2>
<City>Anycity</City>
<StateOrProvinceCode>VA</StateOrProvinceCode>
<PostalCode>24060</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</FDXSubscriptionRequest>"""

values = {'request':xml_request}
headers = { 'Referer' : 'YourCompany',
            'Host':'https://gatewaybeta.fedex.com/GatewayDC',
            'Accept':'image/gif, image/jpeg, image/pjpeg, text/plain,
text/html, */*',
            'Content-Type':'image/gif'
            }

data = urllib.urlencode(values)
req = urllib2.Request(FedEx_API_URL, data, headers)
try:
    handle = urllib2.urlopen(req)
except IOError, e:
    if hasattr(e, 'reason'):
        print 'We failed to reach a server.'
        print 'Reason: ', e.reason
        print e.info()
    elif hasattr(e, 'code'):
        print 'The server couldn\'t fulfill the request.'
        print 'Error code: ', e.code
        print e.info()
else:
    the_page = handle.read()
    print the_page



More information about the Python-list mailing list