Sending a POST request to C# with Python’s requests module
When you send JSON data to C# as a POST request using Python’s Requests module, you may run into a 500 error like the one below.
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing number
The cause is the JSON. To send JSON data via POST with the Requests module, you need to do it like this:
data = {
"id": id,
"no": no
}
headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
response = requests.post(url="http://xxx.com/aaa/api/aaa.aspx", json=data, headers=headers)
When calling post, you have to send the data through the json parameter so that C#’s JSON library doesn’t throw a parsing error!
Leave a comment