encode / httpx

A next generation HTTP client for Python. 🦋
https://www.python-httpx.org/
BSD 3-Clause "New" or "Revised" License
12.7k stars 811 forks source link

502 Error When Using Global Proxy #3195

Closed imdoge closed 1 month ago

imdoge commented 1 month ago

I'm encountering a 502 Bad Gateway error when using httpx to make requests to my local server, but the same requests succeed when using the requests library. Below are the details of the commands I ran and the outputs I received:

import requests import httpx httpx.get("http://localhost:6333/collections") <Response [502 Bad Gateway]> httpx.get("https://www.google.com") <Response [200 OK]> httpx.get("http://httpbin.org/") <Response [200 OK]> requests.get("http://localhost:6333/collections") <Response [200]>

Logs: I enabled logging to further investigate the issue:

import logging logging.basicConfig(level=logging.DEBUG) httpx.get("http://localhost:6333/collections") DEBUG:httpx:load_ssl_context verify=True cert=None trust_env=True http2=False DEBUG:httpx:load_verify_locations cafile='C:\Users\Lin\anaconda3\lib\site-packages\certifi\cacert.pem' DEBUG:httpx:load_ssl_context verify=True cert=None trust_env=True http2=False DEBUG:httpx:load_verify_locations cafile='C:\Users\Lin\anaconda3\lib\site-packages\certifi\cacert.pem' DEBUG:httpx:load_ssl_context verify=True cert=None trust_env=True http2=False DEBUG:httpx:load_verify_locations cafile='C:\Users\Lin\anaconda3\lib\site-packages\certifi\cacert.pem' DEBUG:httpcore.connection:connect_tcp.started host='127.0.0.1' port=233 local_address=None timeout=5.0 socket_options=None DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x0000020FA7EFE4A0> DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'GET']> DEBUG:httpcore.http11:send_request_headers.complete DEBUG:httpcore.http11:send_request_body.started request=<Request [b'GET']> DEBUG:httpcore.http11:send_request_body.complete DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'GET']> DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 502, b'Bad Gateway', [(b'Connection', b'close'), (b'Content-Length', b'0')]) INFO:httpx:HTTP Request: GET http://localhost:6333/collections "HTTP/1.1 502 Bad Gateway" DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'GET']> DEBUG:httpcore.http11:receive_response_body.complete DEBUG:httpcore.http11:response_closed.started DEBUG:httpcore.http11:response_closed.complete

I noticed that the connection is to port 233. I'm using the Clash service mode to globally proxy my network traffic, which seems to be causing the 502 error. However, why do other httpx requests, as well as requests library calls, succeed?

httpx.get("https://www.google.com") <Response [200 OK]> httpx.get("http://httpbin.org/") <Response [200 OK]> requests.get("http://localhost:6333/collections") <Response [200]>

Thanks~