-1

I wanted to know how could I send a curl HTTP Post and store the value of a specific parameter of its curl HTTP response using Python.

So, I first need to execute this HTTP POST in Python which I imagine I will do with the Requests library (correct me if I should better to use another):

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

When I run that curl POST, I get this HTTP response:

{"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}

And I would like to store as a variable, in Python, the specific value of the response parameter "sandbox_init_point" which in this case has a value of "https://sandbox.mercadopago.com.ar/checkout/v1/redirect?pref_id=208040995-2b4ed779-4a0c-4730-8811-1bb194164d1f"

In summary, I would like to know how to execute in Python an HTTP request (curl POST) and store the specific value of a parameter of the corresponding HTTP response.

Thank you so much in advance to you everyone for making coding a lot easier!

2
  • 1
    I think you should read the docs for the requests library. You should post what you have tried in a question and not post until you have researched and attempted some code. Then you can ask a more specific/useful question.
    – OneLiner
    Commented Nov 12, 2020 at 1:39
  • I am not a coder nor a developer, that is why I asked for help in such an early stage... Commented Nov 12, 2020 at 1:45

1 Answer 1

0

Use 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']
4
  • Thank you so much!! I am not a coder and I needed exactly this kind of response step by step. Thank you once again 😃 Commented Nov 12, 2020 at 1:46
  • Sorry, I am getting this error when running the script: AttributeError: 'set' object has no attribute 'items'. Do you know how can I fix it? Commented Nov 12, 2020 at 17:53
  • OK, i updated, just small syntax error, this should work as well
    – gcdsss
    Commented Nov 13, 2020 at 2:17
  • Thank you so much! Commented Nov 13, 2020 at 21:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.