I want to serialize a requests Response
object as json, preferably in HAR format.
import requests
resp = requests.get('http://httpbin.org/get')
har = to_har(resp) # <--- magic
but couldnt find anything online with my google-fu powers.
it seems that all data exist on the Response
object, i hope i dont need to implement the whole HAR spec and there exist some code/utility i can reuse.
a valid answer might give:
existing library or refer to a starting point if nothing exists so far for python
and/or requests
.
currently my simpler 3min solution (not HAR format) serialization to Response
object looks like this (might be good start point if nothing exists):
def resp2dict(resp, _root=True):
d = {
'text': resp.text,
'headers': dict(resp.headers),
'status_code': resp.status_code,
'request': {
'url': resp.request.url,
'method': resp.request.method,
'headers': dict(resp.request.headers),
},
}
if _root:
d['history'] = [resp2dict(h, False) for h in resp.history]
return d
i post this as i think not only me struggle to serialize Response
objects to json in general regardless of HAR format.
currently my simpler 3min solution (not HAR format) serialization to Response object looks like this (might be good start point if nothing exists):
Looks like this is the best solution. I've checked every HAR-related library on PyPI and the only close solution I found (except har2requests) is marshmallow-har. Unfortunately, marshmallow_har.Response.__schema__
match the internal structure neither requests.Response
nor urllib3.response.HTTPResponse
. So, the solutions I see are:
marshmallow-har
can be used.attribute
argument to the fields. I'd advise to fork and extend marshmallow-har
but it uses factories and other strange magic and can't be easily extended. So, it's better to start from ground zero.And consider open-sourcing your solution :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With