[Python] Python requests.post() returns 401 while identical request in Postman succeeds

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm integrating with a third-party shipping API. When I send a POST request to their /api/login-plugin endpoint using Postman, it works perfectly and returns a 200 OK response.

    However, sending the exact same request using Python’s requests module (inside an Odoo backend module) always returns:

    {
    "status": false,
    "code": 401,
    "message": "The authorization key is invalid"
    }

    ✅ What works in Postman:


    URL:
    https://demo.stage.torod.co/en/api/login-plugin

    Headers:

    Content-Type: application/json
    KEY: [masked_key]


    Body (JSON):

    {
    "email": "[user_email]",
    "password": "[user_password]",
    "plugin": "magento",
    "webhook_url": "",
    "site_name": "odoo",
    "site_url": "http://localhost:8069"
    }

    ❌ What fails in Python:


    import requests

    headers = {
    "KEY": "[masked_key]",
    "Content-Type": "application/json"
    }

    payload = {
    "email": "[user_email]",
    "password": "[user_password]",
    "plugin": "magento",
    "webhook_url": "",
    "site_name": "odoo",
    "site_url": "http://localhost:8069"
    }

    response = requests.post("https://demo.stage.torod.co/en/api/login-plugin", headers=headers, json=payload)

    print(response.status_code)
    print(response.text)

    What I’ve tried:

    • Headers and payload match Postman exactly.
    • Tried both json= and data= in the request.
    • Added User-Agent: PostmanRuntime/... to headers.
    • Curl works from the same machine too.
    • No proxy, VPN, or IP restrictions.

    Still, only Python (inside Odoo) fails with 401.

    Any idea what might be causing this mismatch?

    Thanks!

    I tried sending the exact same request from Python (inside Odoo) that works in Postman. I expected a 200 OK response like in Postman, but I consistently get a 401 Unauthorized error. I verified the headers, payload, and even tried mimicking the User-Agent.

    Continue reading...

Compartilhe esta Página