1

I am trying to send a POST JSON request using Python and seem to be getting a 400 error with a parsing error. This same request works fine in Postman..I am unable to understand what I am doing wrong and appreciate any pointers.

Postman request body that works:

{
          "document":
        {
            "name": "TestWebiDOC_doc",
            "folderId":1112223}
} 

Error message that I am getting from the REST Service when using Python:

{"error_code":"400","message":"ParseError at [row,col]:[0,1]\nMessage: A JSONObject text must begin with '{' at character 1 of \"{\\\"document\\\": {\\\"name\\\": \\\"TestWebiDOC_doc\\\", \\\"folderId\\\": \\\"1112223\\\"}}\". "}

Python Code Below to send the JSON request - not able to narrow down what I am doing wrong:

def create_document(token):
    data = {
            'document':
                {
                    'name' : 'TestWebiDOC_doc',
                    'folderId' : '1112223'}
            }
    headers = {'Content-Type' : 'application/json',
               'Accept' : 'application/json',
               'X-SAP-LogonToken' : token}
    path = _url('/documents/')
    response = requests.post(path, json=json.dumps(data), headers=headers)
    print(response.text)
    print('\nURL to create document : {}'.format(path))
    if response.status_code != 200:
        print('\nUnable to create document: {}'.format(response.status_code))
    else:
        document_id = response['success']['id']
        print('\nCreated document with id : {}'.format(document_id))
        return document_id
8
  • 1
    Sorry, I meant to type POST - corrected it now. Commented Mar 18, 2020 at 22:29
  • 1
    Can you try with just: response = requests.post(path, json=data, headers=headers)? Commented Mar 18, 2020 at 22:32
  • Strangely, I tried that before and it failed and it works now...I am now failing at parsing the response....{"success":{"message":"The resource of type \"Document\" with identifier \"6745515\" has been successfully created.","id":"6745515"}} Commented Mar 18, 2020 at 22:35
  • Can you edit the question with the response and what part(s) you want to parse out of it? Commented Mar 18, 2020 at 22:36
  • 1
    Try: response.json()['success']['id'] Commented Mar 18, 2020 at 22:38

1 Answer 1

2

You would just need to pass the dictionary itself as an argument to requests.post, like this:

response = requests.post(path, json=data, headers=headers)

Then to get the ID from the response, do:

response.json()['success']['id']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.