curl 호출을 파이썬 요청으로 변환

PawelS

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"
        }
    ]
}'

그리고 여기에 내가 요청한 내용이 있습니다.

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)   

누구든지 요청과 함께 작동하지 않는 이유를 알고 있습니까? 감사

Martijn Pieters

사이트 는 Location 헤더를 포함하여 302 상태 코드로 응답합니다 .

>>> 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]여기에서 초기 응답이었습니다. 설명서에서 리디렉션 및 기록참조하십시오 .

데이터는 초기 응답에 바로 있습니다 .

사용할 때이 사이트는이 작업을 수행 어떤 Accept-Encoding 헤더; 추가하면 동일한 결과를 얻습니다.

-H "Accept-Encoding: gzip, deflate"

받는 curl명령 또는 행 (값)와 같은 헤더를 추가하여 제공된 payu.apiari.io 콘솔 (클릭 헤더 다음에, 추가 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를 무시하면 정상적인 상태 코드를 방해 할 수 있습니다. 302는 추가 보안 정보가 필요함을 나타낼 수 있습니다 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

curl 요청을 파이썬으로 변환

분류에서Dev

Geoserver cURL을 Python 요청으로 변환

분류에서Dev

cURL을 Python 요청으로 변환

분류에서Dev

curl 호출을 python 요청 호출로 변환하는 방법

분류에서Dev

cURL 요청을 Ruby 게시 요청으로 변환

분류에서Dev

cURL 요청을 Python-Requests 요청으로 변환

분류에서Dev

cURL 헤더 명령을 파이썬 요청으로 번역

분류에서Dev

curl 요청을 Java 요청으로 변환하는 데 도움이 필요합니다.

분류에서Dev

curl을 요청으로 변환하면 400이 제공됩니다.

분류에서Dev

이 curl 명령을 AngularJS $ http 요청으로 어떻게 변환합니까?

분류에서Dev

CURL 요청을 PHP CURL로 변환

분류에서Dev

이 cURL 터미널 요청을 PHP 요청으로 어떻게 변환합니까?

분류에서Dev

curl 명령을 AFNetworking 호출로 변환

분류에서Dev

curl api 호출을 httparty로 변환

분류에서Dev

cURL 호출을 Java로 변환

분류에서Dev

--form을 사용하여 curl을 python 요청으로 변환

분류에서Dev

Ruby : curl 명령을 Ruby net / http 요청으로 변환

분류에서Dev

curl 명령을 요청으로 변환하는 방법

분류에서Dev

JavaScript Get 요청을 PHP Curl로 변환

분류에서Dev

파이썬 서버 요청을 유니티로 변환

분류에서Dev

curl 쿼리를 요청으로 변환

분류에서Dev

cURL을 PHP cURL로 변환

분류에서Dev

curl을 http 요청으로 변환하고 C #에서 응답 받기

분류에서Dev

curl -F 명령을 요청으로 파이썬 코드로 숨기는 방법은 무엇입니까?

분류에서Dev

http : 파이썬 요청에서 동등한 curl 명령으로

분류에서Dev

이 cuRL을 CFHTTP로 변환-Azure NotificationHub

분류에서Dev

Curl 요청을 인증서로 Guzzle 문제로 변환

분류에서Dev

매개 변수 배열이있는 PHP curl POST 요청을 Python 코드로 변환

분류에서Dev

curl 요청을 브라우저 URL로 변환하는 방법

Related 관련 기사

  1. 1

    curl 요청을 파이썬으로 변환

  2. 2

    Geoserver cURL을 Python 요청으로 변환

  3. 3

    cURL을 Python 요청으로 변환

  4. 4

    curl 호출을 python 요청 호출로 변환하는 방법

  5. 5

    cURL 요청을 Ruby 게시 요청으로 변환

  6. 6

    cURL 요청을 Python-Requests 요청으로 변환

  7. 7

    cURL 헤더 명령을 파이썬 요청으로 번역

  8. 8

    curl 요청을 Java 요청으로 변환하는 데 도움이 필요합니다.

  9. 9

    curl을 요청으로 변환하면 400이 제공됩니다.

  10. 10

    이 curl 명령을 AngularJS $ http 요청으로 어떻게 변환합니까?

  11. 11

    CURL 요청을 PHP CURL로 변환

  12. 12

    이 cURL 터미널 요청을 PHP 요청으로 어떻게 변환합니까?

  13. 13

    curl 명령을 AFNetworking 호출로 변환

  14. 14

    curl api 호출을 httparty로 변환

  15. 15

    cURL 호출을 Java로 변환

  16. 16

    --form을 사용하여 curl을 python 요청으로 변환

  17. 17

    Ruby : curl 명령을 Ruby net / http 요청으로 변환

  18. 18

    curl 명령을 요청으로 변환하는 방법

  19. 19

    JavaScript Get 요청을 PHP Curl로 변환

  20. 20

    파이썬 서버 요청을 유니티로 변환

  21. 21

    curl 쿼리를 요청으로 변환

  22. 22

    cURL을 PHP cURL로 변환

  23. 23

    curl을 http 요청으로 변환하고 C #에서 응답 받기

  24. 24

    curl -F 명령을 요청으로 파이썬 코드로 숨기는 방법은 무엇입니까?

  25. 25

    http : 파이썬 요청에서 동등한 curl 명령으로

  26. 26

    이 cuRL을 CFHTTP로 변환-Azure NotificationHub

  27. 27

    Curl 요청을 인증서로 Guzzle 문제로 변환

  28. 28

    매개 변수 배열이있는 PHP curl POST 요청을 Python 코드로 변환

  29. 29

    curl 요청을 브라우저 URL로 변환하는 방법

뜨겁다태그

보관