2016-04-29 110 views
2

我有一個問題轉化捲曲請求轉換成Python的請求捲曲電話撥打:轉換到Python請求

這裏是調用看起來怎麼樣捲曲:(摘自: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" 
     } 
    ] 
}' 

這裏是我在請求中寫道:

import json 
import requests 
headers = { 
    'Content-Type': 'application/json', 
    'Authorization': 'Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd', 
} 

data = { 
    "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" 
     } 
    ] 
} 

resp2 = requests.post('https://secure.payu.com/api/v2_1/orders', headers=headers, json=data) 
print(resp2.json()) 

捲曲作爲響應打印出:

{"orderId":"V6GRPMNRLR160429GUEST000P01","status":{"statusCode":"SUCCESS"},"redirectUri":"https://secure.payu.com/pl/standard/co/summary?sessionId=HtnLqVtBJ5tcOKG2nX03TKwAXOYtXPHe&merchantPosId=145227&timeStamp=1461948331350&showLoginDialog=false&apiToken=9f31fcd1d0d1c5fde8aa57c2b16b5d6bbdfe81543a5f6a12cd39955a487fdaab"} 

而蟒蛇請求:

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

沒有人知道爲什麼它不要求工作?感謝

+1

定義「不工作」,它會給出錯誤嗎? –

+0

當我調用請求時,它不打印出捲曲響應:它應該是「{」orderId「:」V6GRPMNRLR160429GUEST000P01「,」status「:{」statusCode「:」SUCCESS「},」redirectUri「:」https ://secure.payu.com/pl/standard/co/summary?sessionId = HtnLqVtBJ5tcOKG2nX03TKwAXOYtXPHe&merchantPosId = 145227&timeStamp = 1461948331350&showLoginDialog = false&apiToken = 9f31fcd1d0d1c5fde8aa57c2b16b5d6bbdfe81543a5f6a12cd39955a487fdaab「}」。當我做請求時,我得到「simplejson.scanner.JSONDecodeError:期望值:第8行第1列(char 7)」 –

+0

你得到的是一個堆棧跟蹤。將其添加到問題中。 –

回答

1

該網站有302狀態碼,包括Location頭回應:

>>> resp = requests.post('https://secure.payu.com/api/v2_1/orders', headers=headers, json=data) 
>>> resp.history 
[<Response [302]>, <Response [302]>] 
>>> resp.history[0].headers['location'] 
'https://secure.payu.com/pl/standard/co/summary?sessionId=rrQ97fR2bxQJhUqCDORCbRa0uA4WlxJi&merchantPosId=145227&timeStamp=1461950205252&showLoginDialog=false&apiToken=f3599eca78ad55a16d84608e69f3ac7458b782598b064b337be07e8bd6c2c1d5' 
>>> resp.history[0].text 
u'{"orderId":"Z9W2H77TVD160429GUEST000P01","status":{"statusCode":"SUCCESS"},"redirectUri":"https://secure.payu.com/pl/standard/co/summary?sessionId=rrQ97fR2bxQJhUqCDORCbRa0uA4WlxJi&merchantPosId=145227&timeStamp=1461950205252&showLoginDialog=false&apiToken=f3599eca78ad55a16d84608e69f3ac7458b782598b064b337be07e8bd6c2c1d5"}' 

你可以看一下重定向歷史(像我上面那樣)通過查看response.history列表,其中包含先前的請求。這裏最初的迴應是resp.history[0]。請參閱文檔中的Redirection and History

請注意,數據是就有上的初始響應。

當您使用任何Accept-Encoding標題時,該網站會執行此操作;你會得到相同的結果,如果你添加

-H "Accept-Encoding: gzip, deflate" 

curl命令,或通過添加相同的標題(任意值)爲supplied payu.apiari.io console(點擊,然後添加Accept-Encoding頭,並添加值標題)。

你應該認爲這是一個在網站中的錯誤,因爲指定的頭是一個完全正常的事情要做。實際上,如果您自己省略,則Python httplib庫(由requests間接使用)會爲該標頭設置一個默認值,因此這不是requests甚至可以關閉的東西。因此,我已經向PayU報告這是一個錯誤。

作爲變通,你可以使用response.history[0]參考,或者更好的,告訴請求不跟隨重定向擺在首位:

>>> resp = requests.post('https://secure.payu.com/api/v2_1/orders', headers=headers, json=data, allow_redirects=False) 
>>> resp.json() 
{u'orderId': u'NBWTP5WNKK160429GUEST000P01', u'status': {u'statusCode': u'SUCCESS'}, u'redirectUri': u'https://secure.payu.com/pl/standard/co/summary?sessionId=PcvceJHf5En60Dier5gKxCyExiva4qh0&merchantPosId=145227&timeStamp=1461950329704&showLoginDialog=false&apiToken=5962ef901010ca4f8ef6491619217c060f4d53ed0d8e4eadb513d4e0811fc992'} 

但要小心,無視302可能會干擾正常狀態代碼,我看到a 302 may indicate that additional security info is required

+0

謝謝Martijn指出,解決了我的問題。 –