mvantellingen / python-zeep

A Python SOAP client
http://docs.python-zeep.org
Other
1.88k stars 578 forks source link

httpx.HTTPStatusError: Redirect response '302 Moved Temporarily' #1278

Open daeeros opened 2 years ago

daeeros commented 2 years ago
from zeep import Client, Settings
from zeep.transports import AsyncTransport

class Tracker:

    def __init__(self, login, password):

        self.login = login
        self.password = password
        self.api_url = 'https://tracking.russianpost.ru/rtm34?wsdl'

        self.client = Client(
            self.api_url,
            transport=AsyncTransport(),
            settings=Settings(strict=False, xml_huge_tree=True,)
        )

    async def get_history(self, barcode):

        return await self.client.service.getOperationHistory(
            OperationHistoryRequest={
                'Barcode': barcode,
                'MessageType': '0'
            },
            AuthorizationHeader={
                'login': self.login,
                'password': self.password,
            },
        )

tracker = Tracker("", "")

I have this code, but when its try auth, i getting this errror: Traceback (most recent call last):

  File "C:\Users\root\Desktop\telegram-bot\env\lib\site-packages\zeep\transports.py", line 208, in _load_remote_data
    response.raise_for_status()
  File "C:\Users\root\Desktop\telegram-bot\env\lib\site-packages\httpx\_models.py", line 1508, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Redirect response '302 Moved Temporarily' for url 'https://tracking.russianpost.ru/rtm34?wsdl'
Redirect location: 'https://tracking.russianpost.ru/tracking-web-static/rtm34_wsdl.xml'
For more information check: https://httpstatuses.com/302

How can i fix it?

jensenja commented 2 years ago

Since version 0.20.0, httpx no longer follows 3XX redirects in its requests, and instead will raise_for_status() on ANY response that isn't a 2XX.

In order to fix it you have to pass in the follow_redirects=True parameter to your client. This is how I'm doing it:

def client_init(ip, wtype):
    SOAP_USER = "MyUser"
    SOAP_PASSWORD = "MyPass"
    ENDPOINT = 'http://{}:8169/_common/webservices/{}?wsdl'.format(ip, wtype)
    wsdl_client = httpx.Client(auth=(SOAP_USER, SOAP_PASSWORD), follow_redirects=True)
    HAAC = httpx.AsyncClient(auth=(SOAP_USER, SOAP_PASSWORD))
    client = zeep.AsyncClient(
        wsdl=ENDPOINT,
        transport=AsyncTransport(
            client=HAAC,
            wsdl_client=wsdl_client,
            operation_timeout=60,
            cache=InMemoryCache()
        )
    )
    client.settings.xml_huge_tree = True
    return client