如何在Python中获取HTTP响应(在curl POST之后)的特定参数

2024-05-28 20:15:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何发送curlhttppost并使用Python存储其curlhttp响应的特定参数的值

因此,我首先需要在Python中执行此HTTP POST,我想我将使用请求库执行此操作(如果我最好使用另一个请求库,请更正):

 curl -X POST \
  'https://api.mercadopago.com/checkout/preferences' \
  -H 'content-type:application/json' \
  -H 'Authorization: Bearer TEST-6716665210379951-021722-28e8be02433284aa5f78fcc451ba3fec-208040995' \
  -d '{
        "items": [
            {
            "title": "Dummy Item",
            "description": "Multicolor Item",
            "quantity": 1,
            "currency_id": "ARS",
            "unit_price": 10.0
            }
        ]
    }'

当我运行curl POST时,我得到以下HTTP响应:

{"additional_info":"","auto_return":"","back_urls":{"failure":"","pending":"","success":""},"binary_mode":false,"client_id":"6716665210379951","collector_id":208040995,"coupon_code":null,"coupon_labels":null,"date_created":"2020-11-12T01:08:13.385+00:00","date_of_expiration":null,"expiration_date_from":null,"expiration_date_to":null,"expires":false,"external_reference":"","id":"208040995-2b4ed779-4a0c-4730-8811-1bb194164d1f","init_point":"https://www.mercadopago.com.ar/checkout/v1/redirect?pref_id=208040995-2b4ed779-4a0c-4730-8811-1bb194164d1f","internal_metadata":null,"items":[{"id":"","category_id":"","currency_id":"ARS","description":"Multicolor Item","title":"Dummy Item","quantity":1,"unit_price":10}],"marketplace":"NONE","marketplace_fee":0,"metadata":{},"notification_url":null,"operation_type":"regular_payment","payer":{"phone":{"area_code":"","number":""},"address":{"zip_code":"","street_name":"","street_number":null},"email":"","identification":{"number":"","type":""},"name":"","surname":"","date_created":null,"last_purchase":null},"payment_methods":{"default_card_id":null,"default_payment_method_id":null,"excluded_payment_methods":[{"id":""}],"excluded_payment_types":[{"id":""}],"installments":null,"default_installments":null},"processing_modes":null,"product_id":null,"redirect_urls":{"failure":"","pending":"","success":""},
"sandbox_init_point":"https://sandbox.mercadopago.com.ar/checkout/v1/redirect?pref_id=208040995-2b4ed779-4a0c-4730-8811-1bb194164d1f","site_id":"MLA","shipments":{"default_shipping_method":null,"receiver_address":{"zip_code":"","street_name":"","street_number":null,"floor":"","apartment":"","city_name":null,"state_name":null,"country_name":null}},"total_amount":null,"last_updated":null}

我想将响应参数“sandbox_init_point”的特定值存储为一个变量,在Python中,它的值为https://sandbox.mercadopago.com.ar/checkout/v1/redirect?pref_id=208040995-2b4ed779-4a0c-4730-8811-1bb194164d1f“

总之,我想知道如何在Python中执行HTTP请求(curl-POST)并存储相应HTTP响应的参数的特定值

提前非常感谢大家让编码变得更容易


Tags: namehttpscomidhttpnumberdatecode
1条回答
网友
1楼 · 发布于 2024-05-28 20:15:09

使用requests

import requests
headers = {
'Authorization': 'Bearer TEST-6716665210379951-021722-28e8be02433284aa5f78fcc451ba3fec-208040995'
}
data = {
        "items": [
            {
            "title": "Dummy Item",
            "description": "Multicolor Item",
            "quantity": 1,
            "currency_id": "ARS",
            "unit_price": 10.0
            }
        ]
    }
r = requests.post('https://api.mercadopago.com/checkout/preferences', headers=headers, json=data)
r = r.json()
# get response
sandbox_init_point = r['sandbox_init_point']

相关问题 更多 >

    热门问题