Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18242 > unrolled thread

InvalidResponseError: headers must be str

Started byNiklas Rosencrantz <niklasro@gmail.com>
First post2011-12-30 20:58 -0800
Last post2012-01-02 19:18 +0100
Articles 10 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  InvalidResponseError: headers must be str Niklas Rosencrantz <niklasro@gmail.com> - 2011-12-30 20:58 -0800
    Re: InvalidResponseError: headers must be str Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-31 06:40 +0000
      Re: InvalidResponseError: headers must be str Niklas Rosencrantz <niklasro@gmail.com> - 2011-12-31 00:32 -0800
        Re: InvalidResponseError: headers must be str Niklas Rosencrantz <niklasro@gmail.com> - 2011-12-31 00:44 -0800
      Re: InvalidResponseError: headers must be str Serhiy Storchaka <storchaka@gmail.com> - 2011-12-31 12:04 +0200
        Re: InvalidResponseError: headers must be str Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-31 12:52 +0000
          Re: InvalidResponseError: headers must be str Niklas Rosencrantz <niklasro@gmail.com> - 2011-12-31 05:31 -0800
            Re: InvalidResponseError: headers must be str Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-31 14:31 +0000
              Re: InvalidResponseError: headers must be str Niklas Rosencrantz <niklasro@gmail.com> - 2011-12-31 17:40 -0800
                Re: InvalidResponseError: headers must be str "Waldek M." <wm@localhost.localdomain> - 2012-01-02 19:18 +0100

#18242 — InvalidResponseError: headers must be str

FromNiklas Rosencrantz <niklasro@gmail.com>
Date2011-12-30 20:58 -0800
SubjectInvalidResponseError: headers must be str
Message-ID<18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38>
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) )

[toc] | [next] | [standalone]


#18243

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-31 06:40 +0000
Message-ID<4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#18242
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

[toc] | [prev] | [next] | [standalone]


#18245

FromNiklas Rosencrantz <niklasro@gmail.com>
Date2011-12-31 00:32 -0800
Message-ID<3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19>
In reply to#18243
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, in Handle
    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

[toc] | [prev] | [next] | [standalone]


#18247

FromNiklas Rosencrantz <niklasro@gmail.com>
Date2011-12-31 00:44 -0800
Message-ID<29532651.581.1325321090077.JavaMail.geo-discussion-forums@yqlp13>
In reply to#18245
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'}

[toc] | [prev] | [next] | [standalone]


#18248

FromSerhiy Storchaka <storchaka@gmail.com>
Date2011-12-31 12:04 +0200
Message-ID<mailman.4260.1325325872.27778.python-list@python.org>
In reply to#18243
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

[toc] | [prev] | [next] | [standalone]


#18251

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-31 12:52 +0000
Message-ID<4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#18248
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

[toc] | [prev] | [next] | [standalone]


#18252

FromNiklas Rosencrantz <niklasro@gmail.com>
Date2011-12-31 05:31 -0800
Message-ID<9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25>
In reply to#18251
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

[toc] | [prev] | [next] | [standalone]


#18253

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-31 14:31 +0000
Message-ID<4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#18252
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

[toc] | [prev] | [next] | [standalone]


#18285

FromNiklas Rosencrantz <niklasro@gmail.com>
Date2011-12-31 17:40 -0800
Message-ID<19725208.758.1325382015274.JavaMail.geo-discussion-forums@yqja5>
In reply to#18253
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

[toc] | [prev] | [next] | [standalone]


#18336

From"Waldek M." <wm@localhost.localdomain>
Date2012-01-02 19:18 +0100
Message-ID<1ewqzx7xq0r15.dlg@localhost.localdomain>
In reply to#18285
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web