Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload to python-eve using requests

I'm trying to upload a picture to a python-eve server using the requests library. In order to do that, I send a multipart/form-data request. This seems to be a problem for my schema, which looks like this:

schema = {
    'name': {
        'type': 'string',
        'required': True
    },
    'description': {
        'type': 'string'
    },
    'picture': {
        'type': 'media'
    },
     'properties': {
         'type' : 'dict'
     }
}

The request looks like this:

import requests

file = open('/home/user/Desktop/1500x500.jpeg', 'rb')
payload = {'name': 'hello', 'properties': {'status': 'on_hold'}}
r = requests.post("http://localhost:5001/node", data=payload, files={'picture': file})

What I get is a ResourceInvalid exception:

ResourceInvalid: Failed. Response status: 422. Response message: UNPROCESSABLE ENTITY. Error message: {"_status": "ERR", "_issues": {"properties": "must be of dict type"}, "_error": {"message": "Insertion failure: 1 document(s) contain(s) error(s)", "code": 422}}

Is there any solution for this? Am I missing something about the request format?

like image 783
fsiddi Avatar asked Mar 17 '23 11:03

fsiddi


1 Answers

Something like this should work just fine:

import requests

file = open('/home/user/Desktop/1500x500.jpeg', 'rb')
payload = {'name': 'hello'}

r = requests.post("http://localhost:5001/node", data=payload, files={'picture': file})
like image 57
Nicola Iarocci Avatar answered Mar 25 '23 11:03

Nicola Iarocci