vcrpy icon indicating copy to clipboard operation
vcrpy copied to clipboard

AttributeError: 'VCRHTTPResponse' object has no attribute 'json'

Open gschnellOnera opened this issue 10 months ago • 2 comments

I use pytest-recording, to mimic my http requests for testing. When switching to urllib3 to run the queries I encounter the following exception. AttributeError: 'VCRHTTPResponse' object has no attribute 'json'

from urllib3 import PoolManager, make_headers

HTTP_PoolManager = PoolManager()
HTTP_HEADER_AUTH = make_headers(basic_auth='user:password')
req = HTTP_PoolManager.request('GET', url, headers=HTTP_HEADER_AUTH)
if (req.status != 200):
    if req.headers['content-type'].split(';')[0] == "application/json":
        return req.json()
#error managment
retrun None

Versions: python 3.8.10 pytest-recording: 0.13.0 vcrpy: 5.1.0 urllib3: 2.0.4

Workaround:

from urllib3 import PoolManager, make_headers
import json

HTTP_PoolManager = PoolManager()
HTTP_HEADER_AUTH = make_headers(basic_auth='user:password')
req = HTTP_PoolManager.request('GET', url, headers=HTTP_HEADER_AUTH)
if (req.status != 200):
    if req.headers['content-type'].split(';')[0] == "application/json":
        return json.loads(req.data)
#error managment
retrun None

gschnellOnera avatar Oct 02 '23 08:10 gschnellOnera

I might be wrong, but AFAIK, your issue will be solved after vcrpy releases a new version that supports urllib3 v2 -> https://github.com/kevin1024/vcrpy/issues/707

dmelo avatar Nov 02 '23 13:11 dmelo

@gschnellOnera the snippet you shared requires additional work to run in isolation. Could you turn it into a snippet reproducer that can be in isolation easily? Thanks in advance!

hartwork avatar Dec 08 '23 23:12 hartwork