HTTPError... read the response body?

  • Thread starter Stuart Davenport
  • Start date
S

Stuart Davenport

Hi There,

I am trying to connect to a web service but I am getting HTTP 400, I
am not too concerned about the HTTP error - but what I'd like to know
if there is anyway I can read the response body in the HTTP 400 or 500
case? Does the HTTPError allow this? or the urllib2 in anyway?

This is what I have at the moment...

import sys, urllib2
import socket

class APIConnector:
def connect(*args):
length = len(args[2])
headers = {"Content-Length": length, "Content-Type":
"text/xml", "SOAPAction": "\"http://some.api.com/RemovedAction\""}

try:
#url, body, headers
rq = urllib2.Request(args[1], args[3],
headers)
rs = urllib2.urlopen(rq)

print rs.read()
except urllib2.HTTPError, e:
conn = e
print 'error', conn.code, 'message: ',
conn.msg, 'filename: ', conn.filename, 'headers: ', conn.hdrs, 'body:
', conn.read()

I thought that conn.read() may show the body, but it seems not... Can
anyone help please, or suggest another way around? I am using python
2.5 installed with MAC OS X Leopard.


Cheers
Stu
 
S

Stuart Davenport

On Mon, 2 Mar 2009 14:29:12 -0800 (PST), Stuart Davenport wrote:

Hi,
I am trying to connect to a web service but I am getting HTTP 400, I
am not too concerned about the HTTP error - but what I'd like to know
if there is anyway I can read the response body in the HTTP 400 or 500
case? Does the HTTPError allow this? or the urllib2 in anyway?

HTTP error 400 means 'bad request', so it's almost certainly
your mistake.
                        #url, body, headers
                        rq = urllib2.Request(args[1], args[3], headers)

Is args[1] a valid URL? And are you sure that you want to send
something to the web serwer? By specifying the second argument
(args[3]) you're asking python to send HTTP "POST" request,
not "GET".

Hi Wojtek,

Yes, the args[1] is a valid url (I've printed it out to check) and
thats correct, I do want to initiate a POST request by passing over
the SOAP Envelope in the request. All I'd like to know if there is
anyway to read the body of an HTTP Error, so for instance, if I were
to recieve a 404 - I'd like to see the content that the server returns
with a 404 error.
 
D

Diez B. Roggisch

Stuart said:
On Mon, 2 Mar 2009 14:29:12 -0800 (PST), Stuart Davenport wrote:

Hi,
I am trying to connect to a web service but I am getting HTTP 400, I
am not too concerned about the HTTP error - but what I'd like to know
if there is anyway I can read the response body in the HTTP 400 or 500
case? Does the HTTPError allow this? or the urllib2 in anyway?
HTTP error 400 means 'bad request', so it's almost certainly
your mistake.
#url, body, headers
rq = urllib2.Request(args[1], args[3], headers)
Is args[1] a valid URL? And are you sure that you want to send
something to the web serwer? By specifying the second argument
(args[3]) you're asking python to send HTTP "POST" request,
not "GET".

Hi Wojtek,

Yes, the args[1] is a valid url (I've printed it out to check) and
thats correct, I do want to initiate a POST request by passing over
the SOAP Envelope in the request. All I'd like to know if there is
anyway to read the body of an HTTP Error, so for instance, if I were
to recieve a 404 - I'd like to see the content that the server returns
with a 404 error.

HTTPError.read() works for me.

Diez
 

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

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top