RzeroBzero
RzeroBzero

Reputation: 11

JSON Data formatting incorrectly in Python Requests JSON API POST statement

I am trying to post a set of json data to a server. I seem to be missing something because I am getting errors that include the following: (Note:CPAPI is the name of the API)

"SerializationException - could not deserialize "application/json' request using CPAPI.ServiceModule.AddCustomerRequest" \nError: System.Runtime.Serialization.SerializationException:Type definitions should start with a '{', expecting serialized type > > 'AddCustomerRequest' got string starting with: \"{\\"SY_WRKGRP\\":{\\"WRKGRP_ID\\":\\"7\\"},\\"AR_CUST\\":\r\n

Here is my code:

import requests, json

url = "https://shipping:52000/customer/"

payload1 = '{"SY_WRKGRP":{"WRKGRP_ID":"7"},"AR_CUST": {"CUST_NO":"6377123456","NAM":"Rob O","NAM_UPR":"ROBO","FST_NAM":"Rob","FST_NAM_UPR":"ROB","LST_NAM":"O"}}'

headers = {
    "Accept": "application/json",
    "Authorization": "XXXX",
    "APIKey": "YYYY",
    "Content-Type": "application/json"
    }

r = requests.post(url, json=payload1, headers=headers,verify=False)

print()
print(r)
print(r.text)

Any idea what I am missing here?

Upvotes: 1

Views: 954

Answers (1)

jwodder
jwodder

Reputation: 57610

The value passed to requests.post()'s json keyword is supposed to be an unserialized Python value (e.g., a dict), not a serialized JSON string. Either drop the single quotes around payload1 to turn it into a dict or use data=payload1 instead of json=payload1.

Upvotes: 2

Related Questions