InvalidResponseError: headers must be str

Discussion in 'Python' started by Niklas Rosencrantz, Dec 31, 2011.

  1. 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) )
    Niklas Rosencrantz, Dec 31, 2011
    #1
    1. Advertising

  2. On Fri, 30 Dec 2011 20:58:10 -0800, Niklas Rosencrantz wrote:

    > 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.



    --
    Steven
    Steven D'Aprano, Dec 31, 2011
    #2
    1. Advertising

  3. 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
    Niklas Rosencrantz, Dec 31, 2011
    #3
  4. 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'}
    Niklas Rosencrantz, Dec 31, 2011
    #4
  5. 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
    Serhiy Storchaka, Dec 31, 2011
    #5
  6. On Sat, 31 Dec 2011 12:04:13 +0200, Serhiy Storchaka wrote:

    > 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


    Fair point. Your correction is noted.


    --
    Steven
    Steven D'Aprano, Dec 31, 2011
    #6
  7. 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
    Niklas Rosencrantz, Dec 31, 2011
    #7
  8. On Sat, 31 Dec 2011 05:31:14 -0800, Niklas Rosencrantz wrote:

    > 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.


    --
    Steven
    Steven D'Aprano, Dec 31, 2011
    #8
  9. 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
    Niklas Rosencrantz, Jan 1, 2012
    #9
  10. Niklas Rosencrantz

    Waldek M. Guest

    On Sat, 31 Dec 2011 17:40:15 -0800 (PST), Niklas Rosencrantz wrote:
    > 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
    Waldek M., Jan 2, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    470
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    329
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,659
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    296
    Chris
    Aug 11, 2008
Loading...

Share This Page