Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post dictionary value along with a file #1034

Closed
MacLotsen opened this issue Jun 28, 2017 · 2 comments
Closed

Post dictionary value along with a file #1034

MacLotsen opened this issue Jun 28, 2017 · 2 comments
Labels

Comments

@MacLotsen
Copy link

When sending a multipart request as follows:

-----------------------------1996172951399624151317139513
Content-Disposition: form-data; name="attrs"
Content-Type: application/json

{"test":"hello world"}
-----------------------------1996172951399624151317139513
Content-Disposition: form-data; name="files"; filename="myfile.pdf"
Content-Type: application/pdf

<pdf data>

With the following as resource:

'things': {
  'schema': {
    'files': {
      'type': 'list',
      'schema': {'type': 'media'}
    },
    'attrs': {'type': 'dict'}
  }
}

Eve will not detect that the attached json is ment te be of type 'dict'. The exact message:

{_status=ERR, _issues={env=must be of dict type}, _error={code=422.0, message=Insertion failure: 1 document(s) contain(s) error(s)}}

I have seen that it works for string types, yet dictionaries won't work. Is there any workaround present?

Also someone else encountered the problem (see link).

@MacLotsen
Copy link
Author

MacLotsen commented Jul 5, 2017

Got a workaround!
Override the dict validator:

class JsonValidator(Validator):

    def _validate_type_dict(self, field, value):
        if type(value) is dict:
            pass
        try:
            json.loads(value)
        except:
            self._error(value, "Invalid JSON")

app = Eve(validator=JsonValidator)

I know, it's very nasty... Maybe introduce 'converter' so it can be done in one hit?

Add an insert hook:

def multi_request_json_parser(documents):
    for item in documents:
        if 'my_json_field' in item.keys():
            item['my_json_field'] = json.loads(item['my_json_field'])

app.on_insert_myendpoint += multi_request_json_parser

@stale
Copy link

stale bot commented May 11, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 11, 2018
@stale stale bot closed this as completed May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant