lt.languages() returns HTTPError: HTTP Error 405: METHOD NOT ALLOWED
romran opened this issue · 11 comments
Tried to call lt.languages()
and it returned HTTPError: HTTP Error 405: METHOD NOT ALLOWED
Needed to add req.get_method = lambda: 'GET'
into languages()
to make it work.
Hmm, thanks for the report can anyone confirm?
Yes, we see the exact same issue! Hoping for a quick fix :) Thanks!!!
What LibreTranslate mirror are you using?
We have a local docker image using the docker container: libretranslate/libretranslate:latest
But we have not executed an update for a little while so a later version might be available.
In fact I can confirm that there was a later version :). We will try it out...
We used to be on V1.2.7 but should now be on libretranslate 1.2.9.
I can see that the language issue is among the bugs fixed in 1.2.9. :-)
The issue is still there with libretranslatepy-2.1.2
Updating just the libretranslate server version was not enough.
....
File "/home/azureuser/.local/lib/python3.9/site-packages/libretranslatepy/api.py", line 64, in languages
response = request.urlopen(req)
File "/usr/lib64/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python3.9/urllib/request.py", line 523, in open
response = meth(req, response)
File "/usr/lib64/python3.9/urllib/request.py", line 632, in http_response
response = self.parent.error(
File "/usr/lib64/python3.9/urllib/request.py", line 561, in error
return self._call_chain(*args)
File "/usr/lib64/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/usr/lib64/python3.9/urllib/request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 405: METHOD NOT ALLOWED
With the fix suggested by romran, it works perfectly again.
def languages(self):
"""Retrieve list of supported languages.
Returns: A list of available languages ex. [{"code":"en", "name":"English"}]
"""
url = self.url + "languages"
params = dict()
if self.api_key is not None:
params["api_key"] = self.api_key
url_params = parse.urlencode(params)
req = request.Request(url, data=url_params.encode())
# Adding to try to fix bug in call.
req.get_method = lambda: 'GET'
response = request.urlopen(req)
response_str = response.read().decode()
return json.loads(response_str)
We should make the code change to fix this. If anyone's willing to make a pull request I can accept one.
Some questions:
- Is it possible to do this without the lambda? Can we just set the method to GET?
- How widespread this bug is
Yes, it is possible to just set the method in the constructor call for the request object.
I have made a pull request with a fix.
This is now fixed (thanks @MatsBjerin !). It looks like newer versions of LibreTranslate no longer accept POST requests to query the available languages but the old code still works with translate.argosopentech.com which runs an older version of LibreTranslate.
I'm currently having issues accessing PyPI so I won't be able to update the package until they get to my support request but this issue should be fixed in source installations.
Thanks all for the help!
Fixed in libretranslatepy==2.1.3