1

I am creating a charge and I want from it to take its 'hosted_url' in order to redirect a user from page.

I am trying to extract it from json, but I newbie and don't know how

        ...
        url = "https://api.commerce.coinbase.com/charges"

        payload = {
            "local_price": {
                "amount": 1,
                "currency": USDT
            },
            "name": "Test for fun",
            "description": "Project 2",
            "pricing_type": "fixed_price"
        }
        headers = {
            "accept": "application/json",
            "X-CC-Version": "2018-03-22",
            "X-CC-Api-Key" : "**********",
            "content-type": "application/json"
        }

        response = requests.post(url, json=payload, headers=headers)

        print(response.text)

This is a code to make a request to an api in order to create a charge and in JSON Response I get these fields:

{
   "data":{
       "code": "123DVAS",
       "hosted_url": "https://commerce.coinbase.com/charges/123DVAS",
       ...
   }
}

I want somehow to put this message '123DVAS' in variable or to make a redirect like this:

return render(request, 'https://commerce.coinbase.com/charges/123DVAS')
1
  • 1
    Assuming response.text is actually json, you need to use json.loads ... import json d=json.loads(response.text) d would be a dictionary, and d['hosted_url'] should contain the url Commented Oct 5, 2022 at 21:03

1 Answer 1

2

You can do like this:

# Suppose it's your response looks like this
response = {"hosted_url": "https://commerce.coinbase.com/charges/123DVAS"}
id_fecthed = response.get('hosted_url').split('/')[-1]

# You can redner like this
render(request, f'https://commerce.coinbase.com/charges/{id_fecthed}')

# If you sure that always you want to redirect to hosted_url
# Then you can use this
render(request, response.get('hosted_url'))

Edit:

Full code with data read from requests.

import json

response = requests.post(url, json=payload, headers=headers)
data = json.loads(response.text)
hosted_url = data.get("data",{}).get('hosted_url')
if hosted_url:
    id_fecthed = hosted_url.split('/')[-1]
    return redirect(f'https://commerce.coinbase.com/charges/{id_fecthed}')
else:
    print('Host URL not available')
Sign up to request clarification or add additional context in comments.

7 Comments

I have an error: 'Response' object has no attribute 'get'
I did it and appeard an error, 'NoneType' object has no attribute 'split'. How I understood it means that string is empty but when I print(response.text) it is not empty
@Fors1t The data should available in response.json(). Anyways I updated the answer.
I know why hosted_url is empty, it's because my JSON Response is "data" and inside of it, is my information that I need, it is like this: "data" : { "hoster_url": "commerce.coinbase.com/charges/123DVAS" }
@Fors1t This is the issue if you provide half-baked information in the question. Please update the question as well with all the necessary information. Updated answer accordingly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.