By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,382 Members | 2,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,382 IT Pros & Developers. It's quick & easy.

urllib2 - My POST Request just isn't working right

P: n/a
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/wi...rectmanual.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="FDXSubscriptionRequ est.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>ji*@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
Feb 1 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
(blogger.com gives the same error if you don't .strip() your password
before using it to connect)

i noticed one spelling mistake here:
headers = { 'Referer' : 'YourCompany',


'Referrer'

could it be that simple?
Feb 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.