使用Python 3中的请求进行的API调用返回HTML而不是JSON

时间:2019-02-24 20:04:39

标签: python-3.x api curl python-requests payu

我正在使用Python 3.7,并要求2.21.0集成支付网关。

我有以下示例表单沙箱,它可以按预期工作(返回JSON):

curl -X POST https://secure.snd.payu.com/api/v2_1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer d9a4536e-62ba-4f60-8017-6053211d3f47" \
-d '{
    "notifyUrl": "https://your.eshop.com/notify",
    "customerIp": "127.0.0.1",
    "merchantPosId": "300746",
    "description": "RTV market",
    "currencyCode": "PLN",
    "totalAmount": "21000",
    "buyer": {
        "email": "john.doe@example.com",
        "phone": "654111654",
        "firstName": "John",
        "lastName": "Doe",
        "language": "pl"
    },
    "settings":{
        "invoiceDisabled":"true"
    },
    "products": [
        {
            "name": "Wireless Mouse for Laptop",
            "unitPrice": "15000",
            "quantity": "1"
        },
        {
            "name": "HDMI cable",
            "unitPrice": "6000",
            "quantity": "1"
        }
    ]
}'

另一方面,我有以下Python代码

import requests
import json

url = "https://secure.snd.payu.com/api/v2_1/orders"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer d9a4536e-62ba-4f60-8017-6053211d3f47"
}
data = {
    "notifyUrl": "https://your.eshop.com/notify",
    "customerIp": "127.0.0.1",
    "merchantPosId": "300746",
    "description": "RTV market",
    "currencyCode": "PLN",
    "totalAmount": "21000",
    "buyer": {
        "email": "john.doe@example.com",
        "phone": "654111654",
        "firstName": "John",
        "lastName": "Doe",
        "language": "pl"
    },
    "settings":{
        "invoiceDisabled":"true"
    },
    "products": [
        {
            "name": "Wireless Mouse for Laptop",
            "unitPrice": "15000",
            "quantity": "1"
        },
        {
            "name": "HDMI cable",
            "unitPrice": "6000",
            "quantity": "1"
        }
    ]
}

response = requests.post(url, data=json.dumps(data), headers=headers)

但是,我得到的不是HTML的JSON响应。有什么想法吗?在使用Python 2.7时,我使用了urllib并可以使用,但是在Python 3.7中使用urllib进行调用会产生完全相同的效果,即HTML而不是JSON。

响应应该看起来像

{ 
   "status":{ 
      "statusCode":"SUCCESS",
   },
   "redirectUri":"{payment_summary_redirection_url}",
   "orderId":"WZHF5FFDRJ140731GUEST000P01",
   "extOrderId":"{YOUR_EXT_ORDER_ID}",
}

1 个答案:

答案 0 :(得分:0)

添加print_out = db.execute2 "select * from BAR WHERE Value = :info", info print_out.each do |p| puts "%s %s %s" % [p[1],p[2],p[3]] end 就足够了。