You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using curlify to print out curl requests when requests module requests fails, this has always worked great, but now I've moved my requests event hooks to httpx for http2 and asyncio support in our fastapi rest apis.
I believe httpx:Response is a bit different from requests.Response.
if I remove the curlify setting it works all fine
#!/usr/bin/env python3importasyncio, curlify, httpx, json, textwrap# we store each connectionconnection=Noneasyncdefhook(response: httpx.Response) ->None:
""" requests hook to see full rest requests+curl+response into connection_trace global, must not return anything """format_headers=lambdad: '\n'.join(f'{k}: {v}'fork, vind.items())
globalconnectionout=''awaitresponse.aread()
print(f"response.{response}")
request=response.requestifnothasattr(request, 'body'):
setattr(request, 'body', None)
ifnothasattr(response, 'reason'):
setattr(response, 'reason', None)
mycurl=curlify.to_curl(request)
try:
ifresponse.text:
out=json.dumps(json.loads(response.text), indent=2, sort_keys=True)
exceptjson.JSONDecodeError:
out=response.textconnection=textwrap.dedent(''' ---------------- request ---------------- {req.method} {req.url} {reqhdrs} {req.body} ------------------ curl ------------------ {mycurl} ---------------- response ---------------- {res.status_code} {res.reason} {res.url} {reshdrs} {out} ----------------------------------------- ''').format(
req=request,
res=response,
mycurl=mycurl,
reqhdrs=format_headers(request.headers),
reshdrs=format_headers(response.headers),
out=out
)
c=httpx.AsyncClient(event_hooks={'response': [hook]})
#c = httpx.AsyncClient()r=asyncio.run(c.get("https://cat-fact.herokuapp.com/facts"))
print(connection)
We are using curlify to print out curl requests when requests module requests fails, this has always worked great, but now I've moved my requests event hooks to httpx for http2 and asyncio support in our fastapi rest apis.
I believe
httpx:Response
is a bit different from requests.Response.if I remove the curlify setting it works all fine
I get this error:
removing the curlify it all works excellent: (it is a pubøic open api):
The text was updated successfully, but these errors were encountered: