Daniel
Daniel

Reputation: 287

Django and requests JSON response

How do I get JSON response in django using requests library? I currently have something of the following form:

    def predict(self, form):
        image = form.instance.input_image
        image_data = bytes(image.read())
        img = Image.open(io.BytesIO(image_data))
        data_dict = {'input': np.array(img).tolist()}
        data = requests.post(
            'http://localhost:5000/predict', json=data_dict)
        return JsonResponse({'prediction': list(data)})

but this just returns <Response [200]> and the error TypeError: Object of type bytes is not JSON serializable?

Upvotes: 0

Views: 356

Answers (1)

token
token

Reputation: 933

Not sure if I understand your problem, but:

first thing:

data = requests.post('http://localhost:5000/predict', data=json.dumps(data_dict))

then, if you want requests response as json, simply:

data.json()

Upvotes: 3

Related Questions