-1
import requests
import datetime

server_addr = "https://api-3t.paypal.com/nvp"

headers = {'content-type': 'text/plain'}

params = {
    'method'        :'BMButtonSearch',
    'version'       :'124.0',
    'user'          :'xxx',
    'pwd'           :'xxx',
    'signature'     :'xxx',
    'startdate'     :datetime.datetime.now().isoformat()
}

r = requests.post(server_addr, headers=headers, data=params)
r.encoding='utf-8'
print(r.status_code)
print(r.text)

200

TIMESTAMP=2016%2d04%2d07T04%3a40%3a13Z&CORRELATIONID=1b4a043b2afe&ACK=Success&VERSION=124%2e0&BUILD=000000

r.text u'TIMESTAMP=2016%2d04%2d07T04%3a40%3a13Z&CORRELATIONID=1b4a043b2afe&ACK=Success&VERSION=124%2e0&BUILD=000000' <-- This isn't decoded or broken into key/value pairs as expected. It is just one large string.

Additional Information:

r.encoding
'utf-8'

r.headers
{'Content-Length': '106', 'Set-Cookie': 'X-PP-SILOVER=name%3DLIVE11.APIT.1%26silo_version%3D880%26app%3Dappdispatcher_apit%26TIME%3D2917401943; domain=.paypal.com; path=/; Secure; HttpOnly, X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT', 'Server': 'Apache', 'Connection': 'close', 'Paypal-Debug-Id': '1b4a043b2afe', 'X-PAYPAL-OPERATION-NAME': 'BMButtonSearch', 'Date': 'Thu, 07 Apr 2016 04:40:13 GMT', 'X-PAYPAL-API-RC': '', 'Content-Type': 'text/plain; charset=utf-8', 'HTTP_X_PP_AZ_LOCATOR': 'dcg12.slc'}
ArK
  • 20,698
  • 67
  • 109
  • 136
NFN_NLN
  • 45
  • 1
  • 6
  • 1
    Why would that be expected? [The docs](http://docs.python-requests.org/en/v0.10.6/api/#requests.Response.text) don't mention anything about `text` doing any parsing. – magni- Apr 07 '16 at 04:57
  • Requests will automatically decode content from the server. --> http://docs.python-requests.org/en/master/user/quickstart/ – NFN_NLN Apr 07 '16 at 05:30
  • You're confusing decoding with parsing. Note the example: `>>> r.text => u'[{"repository":{"open_issues":0,"url":"https://github.com/...` That's a string you get back, not a dictionary. – magni- Apr 07 '16 at 05:35
  • You just quoted example output which looks nothing like the output I get. Therefore the output I get is unexpected. It is neither decoded nor parsed. You are seeing the %2d which are clear examples of URL encoding. – NFN_NLN Apr 07 '16 at 05:44
  • It's URL-encoded, which isn't the kind of decoding Requests does by default. To decode and parse in one step, look at my answer below, which works on the string in your question. `This isn't ... as expected. It is just one large string.` That's exactly what's expected when you call `text` on a `Response` object. – magni- Apr 07 '16 at 05:49

1 Answers1

0

Since that format looks like a query string, you can use urlparse from the standard library to parse it:

print(urlparse.parse_qs(r.text))

Edit: If all you want is to unencode the string (but leave it as a string), use urllib.unquote:

print(urllib.unquote(r.text))

(see this answer here: How to decode a 'url-encoded' string in python)

Community
  • 1
  • 1
magni-
  • 1,934
  • 17
  • 20