0

I have the following code

import requests


headers = {
    'Host': 'extranet-lv.bwfbadminton.com',
    'Content-Length': '0',
    'Sec-Ch-Ua': '"Chromium";v="123", "Not:A-Brand";v="8"',
    'Accept': 'application/json, text/plain, */*',
    'Content-Type': 'application/json;charset=UTF-8',
    'Sec-Ch-Ua-Mobile': '?0',
    'Authorization': 'Bearer 2|NaXRu9JnMpSdb8l86BkJxj6gzKJofnhmExwr8EWkQtHoattDAGimsSYhpM22a61e1crjTjfIGTKfhzxA',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.122 Safari/537.36',
    'Sec-Ch-Ua-Platform': '"macOS"',
    'Origin': 'https://match-centre.bwfbadminton.com',
    'Sec-Fetch-Site': 'same-site',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://match-centre.bwfbadminton.com/',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
    'Priority': 'u=1, i'
}


def get_tid() -> str:
    URL = f'https://extranet-lv.bwfbadminton.com/api/vue-current-live'

    r = requests.post(URL, headers=headers, json={'drawCount': '0'})

    if r.status_code == 200:
        encoded_r = r.text.encode('utf-8')

The output should look as follows:

{"results":[{"id":4746,"code":"DAC5B0C1-A817-4281-B3C6-F2F3DA65FD2B","name":"PERODUA Malaysia Masters 2024", ...}

If I'm not mistaken, the output of r.text consists of unicode characters:

enter image description here

How do I transform this to the desired dictionary?

1
  • In your own words, when you connect to this API endpoint, why do you believe that the result should be comprehensible text, in any encoding? Also, are you quite certain it's okay to publicize that Authorization value? Commented May 25, 2024 at 8:14

3 Answers 3

1

This has little to do with Unicode. You’re simply looking at binary gibberish as text. Why do you get binary gibberish instead of the expected JSON? Likely because you claim to be ready to accept binary gibberish:

'Accept-Encoding': 'gzip, deflate, br'

The server is likely compressing the response as gzip or such, because you’re claiming to be okay with it. But actually you’re not, so remove that line from your request headers.

Sign up to request clarification or add additional context in comments.

Comments

0

If you simplify your headers to only contain Authorization you can do this:

import requests
import json

headers = {
    "Authorization": "Bearer 2|NaXRu9JnMpSdb8l86BkJxj6gzKJofnhmExwr8EWkQtHoattDAGimsSYhpM22a61e1crjTjfIGTKfhzxA"
}

url = "https://extranet-lv.bwfbadminton.com/api/vue-current-live"
js = {"drawCount": "0"}

with requests.post(url, json=js, headers=headers) as response:
    response.raise_for_status()
    print(json.dumps(response.json(), indent=2))

Comments

-2

I got the result Response content is not valid JSON: error. You may inspect the result as follow:

def get_tid() -> str:
    URL = 'https://extranet-lv.bwfbadminton.com/api/vue-current-live'
    
    try:
        r = requests.post(URL, headers=headers, json={'drawCount': '0'})
        r.raise_for_status()  # Check if the request was successful
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
        return
    except Exception as err:
        print(f"Other error occurred: {err}")
        return
    
    try:
        text = r.json()  # Attempt to parse the JSON response
        print(text)
    except requests.exceptions.JSONDecodeError:
        print("Response content is not valid JSON:")
        print(r.text)  # Print the raw response text for debugging

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.