I try to send async PUT request with AsyncHTTPClient, but tornado return HTTP 599 error before request_timeout.
I fix this problem by write body="" to request:
req = tornado.httpclient.HTTPRequest(url,
method="PUT",
request_timeout=10,
headers=headers,
**body=""**)
Tornado can't send put request with empty body.
Related
Using the library httpx which allows me to make HTTP/2 request to target sites.
However when I use the proxy it seem to automatically set my request to HTTP/1.
I.e
async def main():
client = httpx.AsyncClient(http2=True)
response = await client.get('someurl', headers=headers)
print(response.http_version)
This prints HTTP/2
But same thing using proxy like so client = httpx.AsyncClient(http2=True, proxies=someproxydictionary)
it prints HTTP/1
Why is this behavior happening only when routing through proxies?
I'm using FastAPI with aiohttp, I built a singleton for a persistent session and I'm using it for opening the session at startup and closing it at shutdown.
Demand: The response body is precious in case of a failure I must log it with the other details.
Because how raise_for_status behave I had to write those ugly functions which handle each HTTP method, this is one of them:
async def post(self, url: str, json: dict, headers: dict) -> ClientResponse:
response = await self.session.post(url=url, json=json, headers=headers)
response_body = await response.text()
try:
response.raise_for_status()
except Exception:
logger.exception('Request failed',
extra={'url': url, 'json': json, 'headers': headers, 'body': response_body})
raise
return response
If I could count on raise_for_status to return also the body (response.text()),
I just could initiate the session ClientSession(raise_for_status=True) and write a clean code:
response = await self.session.post(url=url, json=json, headers=headers)
Is there a way to force somehow raise_for_status to return also the payload/body, maybe in the initialization of the ClientSession?
Thanks for the help.
It is not possible for aiohttp and raise_for_status. As #Andrew Svetlov answered here:
Consider response as closed after raising an exception.
Technically it can contain a partial body but there is no any guarantee.
There is no reason to read it, the body could be very huge, 1GiB is not a limit.
If you need a response content for non-200 -- read it explicitly.
Alternatively, consider using the httpx library in this way. (It is widely used in conjunction with FastAPI):
def raise_on_4xx_5xx(response):
response.raise_for_status()
async with httpx.AsyncClient(event_hooks={'response': [raise_on_4xx_5xx]}) as client:
try:
r = await client.get('http://httpbin.org/status/418')
except httpx.HTTPStatusError as e:
print(e.response.text)
I'm using python3 and aiohttp http server to handle requests from client.How should I redirect a POST request with body data?
I've tried
aiohttp.web.HTTPFound('/redirect')
it works with GET requests,but what if the request is POST?
You can redirect request with body, headers, etc.
HTTPFound(location, *, headers=None, reason=None, body=None, text=None, content_type=None)
See documentation: https://docs.aiohttp.org/en/stable/web_quickstart.html#exceptions
We are using aiohttp to make multiple requests to various website vendors to grab their latest data.
Some of the content providers serve the data from a cache. Is it possible to request the data from the server directly? We have tried to pass in the headers parameter with no luck.
async def fetch(url):
global response
headers = {'Cache-Control': 'no-cache'}
async with ClientSession() as session:
async with session.get(url, headers=headers, proxy="OUR-PROXY") as response:
return await response.read()
The goal is to get the last-modified date header, which is not provided from the cache request.
Try to add some additional variable with dynamic value to URL (e.g. timestamp).
This will prevent caching on the server side even if it ignores Cache-Control.
Example:
from: https://example.com/test
to: https://example.com/test?timestamp=20180724181234
I have this part of code in Tornado:
............
como_url = "".join(['http://', options.como_address, ':', options.como_port,
'/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s'])
http_client = AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, como_url)
ret = {}
if response.error:
ret['error'] = 'Error while retrieving the response'
self.write(tornado.escape.json_encode(ret))
self.finish()
else:
.........
I send a command to a sensor and after I do an http request to a db to retrieve the response from sensor.
And after I take the response and do some actions.
Now, I have to send the http request two times to retrieve the response. The first time I send a command to the sensor, is something like the request is too fast to receive the response.
After I resend the command, the response is found right and all is fine.
Why? How can I set I don't know a timeout to the request... or how can I resend in Tornado the request two times?
Thank you.
EDIT
I wrote like this:
como_url = "".join(['http://', options.como_address, ':', options.como_port,
'/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s'])
http_client = AsyncHTTPClient()
#response = yield tornado.gen.Task(http_client.fetch, como_url)
request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=5.0, request_timeout=2.0)
response = yield tornado.gen.Task(http_client.fetch, request)
print response
and I obtain:
HTTPResponse(code=200,request_time=0.30609703063964844,buffer=<io.BytesIO object at 0x276a1d0>,_body=None,time_info={},request=<tornado.httpclient.HTTPRequest object at 0x2764310>,effective_url='http://131.114.52.207:44444/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s',headers={'Content-Type': 'text/plain'},error=None)
Why request_time=0????