InvalidResponseError: headers must be str

N

Niklas Rosencrantz

I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get this error:

InvalidResponseError: headers must be str

I think it is because somewhere my HTTP headers are cast to unicode instead of string but I can't find where in the code? The example code I'm trying to upgrade to python 2.7 is https://github.com/supernifty/gae-paypal-online-market-example

class Pay( object ):
def __init__( self, amount, return_url, cancel_url, remote_address, secondary_receiver=None, ipn_url=None, shipping=False ):
headers = {
'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID),
'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD),
'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE),
'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'),
'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'),
'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID),
'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address),
}

data = {
'currencyCode': 'USD',
'returnUrl': return_url,
'cancelUrl': cancel_url,
'requestEnvelope': { 'errorLanguage': 'en_US' },
}

if shipping:
data['actionType'] = 'CREATE'
else:
data['actionType'] = 'PAY'

if secondary_receiver == None: # simple payment
data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%f' % amount } ] }
else: # chained
commission = amount * settings.PAYPAL_COMMISSION
data['receiverList'] = { 'receiver': [
{ 'email': settings.PAYPAL_EMAIL, 'amount': '%0.2f' % amount, 'primary': 'true' },
{ 'email': secondary_receiver, 'amount': '%0.2f' % ( amount - commission ), 'primary': 'false' },
]
}

if ipn_url != None:
data['ipnNotificationUrl'] = str(ipn_url)

self.raw_request = json.dumps(data)
#request = urllib2.Request( "%s%s" % ( settings.PAYPAL_ENDPOINT, "Pay" ), data=self.raw_request, headers=headers )
#self.raw_response = urllib2.urlopen( request ).read()
self.raw_response = url_request( "%s%s" % ( str(settings.PAYPAL_ENDPOINT), str("Pay") ), data=self.raw_request, headers=headers ).content()
logging.info( "response was: %s" % self.raw_response )
self.response = json.loads( str(self.raw_response) )
 
S

Steven D'Aprano

I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get
this error:

InvalidResponseError: headers must be str

Please show the ENTIRE traceback, not just the error message. The
traceback is very informative: it shows the actual line of code that
causes the problem, plus the entire sequence of calls that lead to it.
The error message on its own is almost useless.

Please COPY AND PASTE the traceback, do not re-type it from memory,
summarize it, simplify it, or otherwise change it in any way.

I think it is because somewhere my HTTP headers are cast to unicode

Why do you think that?

instead of string but I can't find where in the code?

Have you tried searching for any of these?

- direct calls to unicode()
- unicode literals u"..."
- calls to the decode method some_string.decode( ... )


The example code
I'm trying to upgrade to python 2.7 is
https://github.com/supernifty/gae-paypal-online-market-example

class Pay( object ):
def __init__( self, amount, return_url, cancel_url, remote_address,
secondary_receiver=None, ipn_url=None, shipping=False ):
headers = {
'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID),
'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD),
'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE),
'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'),
'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'),
'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID),
'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address),
}


'JSON' is already a string. Calling str() on it is a waste of time.

What values do the various settings.* have? If they are already strings,
calling str() again is a waste of time. I see you do this all through
your class, needlessly calling str() on strings.
 
N

Niklas Rosencrantz

Thank you for the reply. I had the same error message before and it was resolved when I removed a casting of a header value from unicode to str. Now in this case I can't see where that happens or what causes the error. The full trace I'm experiencing now is

File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in_StartResponse
raise InvalidResponseError('headers must be str')
InvalidResponseError: headers must be str
INFO 2011-12-31 04:55:36,484 recording.py:372] Saved; key: __appstats__:034800, part: 137 bytes, full: 27325 bytes, overhead: 0.001 + 0.008; link:http://localhost:8080/_ah/stats/details?time=1325307334890
ERROR 2011-12-31 04:55:36,484 wsgi.py:186]
Traceback (most recent call last):
File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 175, inHandle
for chunk in result:
File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 926, in appstats_wsgi_wrapper
result = app(environ, appstats_start_response)
File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1524, in __call__
return self._internal_error(e)(environ, start_response)
File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1522, in __call__
return response(environ, start_response)
File "/media/Lexar/montao/lib/webob/webob/__init__.py", line 2000, in __call__
start_response(self.status, self.headerlist)
File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 923, in appstats_start_response
return start_response(status, headers, exc_info)
File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in_StartResponse
raise InvalidResponseError('headers must be str')
InvalidResponseError: headers must be str
 
N

Niklas Rosencrantz

I can log the headers and it seems that they are strings:

INFO 2011-12-31 08:43:03,286 paypal.py:143] headers: {'X-PAYPAL-REQUEST-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-PASSWORD': '1324348659', 'X-PAYPAL-RESPONSE-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-SIGNATURE': 'Al6H7Bq3kp4bKFht2fR-p2FlAbp3AJFKl5pFMzHpo.QKYe4Uob5YPIm.', 'X-PAYPAL-SECURITY-USERID': 'niklas_1354859649_biz_api1.eddaconsult.se', 'X-PAYPAL-DEVICE-IPADDRESS':'127.0.0.1', 'X-PAYPAL-APPLICATION-ID': 'APP-80W284485P519543T'}
 
S

Serhiy Storchaka

31.12.11 08:40, Steven D'Aprano напиÑав(ла):
'JSON' is already a string. Calling str() on it is a waste of time.

from __future__ import unicode_literals
 
S

Steven D'Aprano

31.12.11 08:40, Steven D'Aprano напиÑав(ла):

from __future__ import unicode_literals

Fair point. Your correction is noted.
 
N

Niklas Rosencrantz

I'm still no further to reaching a solution and my efforts of logging the headers didn't produce anything. I'm certain that it's upgrading from ptyhon2.5 to python 2.7 that causes this since the GAE SDK uses WSGI instead of CGI now. Any idea about my problem?

Thank you
 
S

Steven D'Aprano

I'm still no further to reaching a solution and my efforts of logging
the headers didn't produce anything. I'm certain that it's upgrading
from ptyhon 2.5 to python 2.7 that causes this since the GAE SDK uses
WSGI instead of CGI now. Any idea about my problem?

Thank you

Have patience. It has been less than a day since you first asked, and it
is New Years Day or New Years Eve (depending on where you are). Most
people will be away from their computers or busy celebrating.
 
N

Niklas Rosencrantz

Thanks for the replies here. I will have patience but this bug is blocking my integration efforts. I tried logging the TCP packets with tcpdump and nothing special appeared.

Niklas
 
W

Waldek M.

Thanks for the replies here. I will have patience but this bug
is blocking my integration efforts. I tried logging the TCP
packets with tcpdump and nothing special appeared.

Well, it's free software, isn't it?
You may either wait a little, fix it yourself or pay someone
to fix it for you, if you're in a hurry. Or stay with 2.5 / try upgrading
to 3.x.

The Python community is usually very helpful and friendly, but
by *demanding& a fix ASAP you may accidentily step on people's toes :)

Best regards,
Waldek
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top