Search code examples
pythonrequestrequest-headershttpx

Request fails when using httpx in python but is successful using Request


I am trying to send an api request using httpx and it is failing. When I try to send the same request using request, it works fine. What could be causing this?

Using Request

import requests

headers = {
    'accept-language': 'en-US',
    'content-type': 'application/json',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
}

response = requests.get('https://www.unibet.com/sportsbook-feeds/views/filter/baseball/all/matches', headers=headers)

print(response.status_code)

Output 200

Using httpx:

import httpx

headers = {
    'accept-language': 'en-US',
    'content-type': 'application/json',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
}

response = httpx.get('https://www.unibet.com/sportsbook-feeds/views/filter/baseball/all/matches', headers=headers)

print(response.status_code)

Output 503

What I have tried:

My guess is that httpx defaults certain query parameters in a different way than request does. I have tried to set all parameters the same way on each side, without success.

url = 'https://www.unibet.com/sportsbook-feeds/views/filter/basketball/all/matches', 
        method = 'GET', 
        headers = headers,
        params = None,
        data = None,
        files = None,
        json = None,
        cookies = None,
        auth = None,
        timeout = None

Solution

  • Thanks to chitown 83, I have found the solution to my issue.

    It seems HTTPx had issue with the certificate. I was able to fix my problem by assigning a default context to my client using below code sample.

    import httpx, ssl, certifi
    context = ssl.create_default_context()
    context.load_verify_locations(certifi.where()) 
    client = httpx.Client(verify=context)  
    client.get('https://example.com')