[Python] Why is my HTTP proxy not forwarding headers correctly in requests?

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to route my Python application's traffic through an HTTP proxy, but some headers appear to be getting dropped or modified. Here's my setup:

    import requests

    proxies = {
    'http': 'http://user:pass@proxy.example.com:3128',
    'https': 'http://user:pass@proxy.example.com:3128'
    }

    headers = {
    'Authorization': 'Bearer my_token',
    'Custom-Header': 'ImportantValue',
    'Content-Type': 'application/json'
    }

    response = requests.post(
    'https://api.target-service.com/data',
    json={'key': 'value'},
    headers=headers,
    proxies=proxies
    )


    The target service receives the request but reports missing Authorization header. The Custom-Header is also missing. Basic connectivity works (the request reaches the destination).

    I've tried to verify the proxy works with simple GET requests and confirme the headers are present when making direct requests (without proxy). And I also tried with both requests and urllib3 libraries

    Added Proxy-Connection: keep-alive header (no effect)

    Environment:

    • Python 3.10
    • requests 2.28.1
    • Corporate HTTP proxy (Squid)

    What could be causing the proxy to strip these headers? Are there specific proxy configurations that would explain this behavior, or is there a way to enforce header preservation?

    Continue reading...

Compartilhe esta Página