我在将curl请求转换为Python请求调用时遇到问题:
以下是curl调用的外观:(取自:http://developers.payu.com/en/restapi.html#creating_new_order_api)
curl -X POST https://secure.payu.com/api/v2_1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd" \
-d '{
"notifyUrl": "https://your.eshop.com/notify",
"customerIp": "127.0.0.1",
"merchantPosId": "145227",
"description": "RTV market",
"currencyCode": "PLN",
"totalAmount": "21000",
"products": [
{
"name": "Wireless Mouse for Laptop",
"unitPrice": "15000",
"quantity": "1"
},
{
"name": "HDMI cable",
"unitPrice": "6000",
"quantity": "1"
}
]
}'
下面是我在请求中写的:
^{pr2}$卷曲作为回应打印出:
{"orderId":"V6GRPMNRLR160429GUEST000P01","status":{"statusCode":"SUCCESS"},"redirectUri":"https://secure.payu.com/pl/standard/co/summary?sessionId=HtnLqVtBJ5tcOKG2nX03TKwAXOYtXPHe&merchantPosId=145227&timeStamp=1461948331350&showLoginDialog=false&apiToken=9f31fcd1d0d1c5fde8aa57c2b16b5d6bbdfe81543a5f6a12cd39955a487fdaab"}
而python请求:
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Traceback (most recent call last):
File "/home/ubuntu/workspace/src/billing/testapi.py", line 30, in <module>
print(resp2.json())
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 808, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 488, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 389, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
有人知道为什么它不能处理请求吗?谢谢
站点以一个302状态代码响应,其中包括一个位置标头:
您可以通过查看
response.history
列表来查看重定向历史记录(就像我上面所做的那样),该列表包含前面的请求。resp.history[0]
是这里的最初反应。请参阅文档中的Redirection and History。在请注意,数据就在初始响应上。在
当您使用任何
^{pr2}$Accept-Encoding
头时,站点会这样做;如果您添加添加到
curl
命令,或将相同的头(具有任何值)添加到supplied payu.apiari.io console(单击Headers,然后添加Accept-Encoding
头并为头添加一个值)。在您应该认为这是站点中的一个bug,因为指定header是一个非常正常的操作。事实上,Python}可以关闭的。因此,我已将此报告给PayU。在
httplib
库(由requests
间接使用)会为该头设置一个默认值,如果您自己忽略它,那么这不是{作为解决方法,您可以使用
response.history[0]
引用,或者更好地告诉请求不要首先遵循重定向:但是注意,忽略302可能会干扰正常状态代码,我看到a 302 may indicate that additional security info is required。在
相关问题 更多 >
编程相关推荐