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

PATCH Bad Request (400) Using Requests #1274

Closed
masrayie opened this issue May 27, 2019 · 4 comments
Closed

PATCH Bad Request (400) Using Requests #1274

masrayie opened this issue May 27, 2019 · 4 comments
Milestone

Comments

@masrayie
Copy link

@masrayie masrayie commented May 27, 2019

Environment

  • Python version: 3.7
  • Eve version: latest

iam using requests.patch
i've added If-Match headers on headers like this

headers = {'Content-Type': 'application/json', 'If-Match' : '741372f7206cb126aa465c720ba90d2312bfb308'}

data = {'field_name' : value}
response = requests.patch(api_url, data= data, auth=('uname', 'pswd'), headers=headers)

but i got 400 bad request
could somebody help me ?
or my codes are wrong ?

Thanks !

@nicolaiarocci
Copy link
Member

@nicolaiarocci nicolaiarocci commented May 28, 2019

If you set debug mode on, you are likely to get back an error explanation in the body of the response. See if that helps.

@gwainer
Copy link

@gwainer gwainer commented May 31, 2019

I've found HTTP 400 error when patching a sub-document, If the key being patched is nullable and still does not exist in the document being patched.

Eve 0.9.1
Python 3.5

Note: Using eve 0.8.1 in same scenario, the error does not occur.

The error stack is:

[2019-05-31 14:01:29,019] ERROR in patch: 'test'
Traceback (most recent call last):
  File "path/lib/python3.5/site-packages/eve/methods/patch.py", line 179, in patch_internal
    updates, object_id, original, normalize_document
  File "path/lib/python3.5/site-packages/eve/validation.py", line 44, in validate_update
    document, update=True, normalize=normalize_document
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 870, in validate
    self.__normalize_mapping(self.document, self.schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 617, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 680, in __normalize_containers
    field, mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 731, in __normalize_mapping_per_schema
    always_return_document=True)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 594, in normalized
    self.__normalize_mapping(self.document, self.schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 617, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 680, in __normalize_containers
    field, mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 731, in __normalize_mapping_per_schema
    always_return_document=True)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 594, in normalized
    self.__normalize_mapping(self.document, self.schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 617, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 680, in __normalize_containers
    field, mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 731, in __normalize_mapping_per_schema
    always_return_document=True)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 594, in normalized
    self.__normalize_mapping(self.document, self.schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 617, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 680, in __normalize_containers
    field, mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 731, in __normalize_mapping_per_schema
    always_return_document=True)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 594, in normalized
    self.__normalize_mapping(self.document, self.schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 615, in __normalize_mapping
    self.__normalize_default_fields(mapping, schema)
  File "path/lib/python3.5/site-packages/cerberus/validator.py", line 806, in __normalize_default_fields
    self._normalize_default(mapping, schema, field)
  File "path/lib/python3.5/site-packages/eve/validation.py", line 71, in _normalize_default
    challenge = challenge[sub_field]
KeyError: 'test'

Minimum schema for example is like this:

'export_data': {
    'type': 'dict',
    'schema': {
        'test': {
            'type': 'dict',
            'schema': {
                'codes': {
                    'type': 'dict',
                    'schema': {
                        'extra_50': {
                            'type': 'dict',
                            'schema': {
                                'code': {'type': 'integer', 'nullable': True}
                            }
                        },
                    }
                }
            }
        }
    }
}

PATCH body is:

{
    "export_data": {
        "test": {
            "codes": {
                "extra_75": {
                    "code": 98
                }
            }
        }
    }
}

If code key is changed to nullable: False or test key already exists in the document, the error does not happen

@sunbit
Copy link
Contributor

@sunbit sunbit commented Jun 5, 2019

@gwainer can you try with eve from this branch https://github.com/sunbit/eve/tree/fix_put_normalize_%231280

I have a pending PR #1280 to approve, that fixed just that lines of code that are giving you the problem.

@gwainer
Copy link

@gwainer gwainer commented Jun 5, 2019

@sunbit I've just tested and I'm happy to inform that it solves my scenario too. Thanks a lot for your work.

@nicolaiarocci nicolaiarocci added this to the 0.9.2 milestone Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants