InvalidResponseError: headers must be str

Niklas Rosencrantz niklasro at gmail.com
Fri Dec 30 23:58:10 EST 2011


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



More information about the Python-list mailing list