urllib2 - My POST Request just isn't working right

  • Thread starter =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
  • Start date
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

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>[email protected]</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
 
D

Don't Code Faster Than You Can See

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

POST from a CGI 5

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top