Python Requests

Hi, I have a POST and a GET for the SlotCfgMode
The POST fails but the GET works
The POST fails with the message,
“Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported”
can’t figure out how to fix the error

Please see attachment:

Hi, I have a POST and a GET for the SlotCfgMode

The POST fails but the GET works

The POST fails with the message,

“Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported”

I can’t figure out how to fix the error

filename is Imports.py from the same directory

global mytoken
import requests
import json
import warnings
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

*************************************************************

GET TOKEN

*************************************************************

urltoken = “https://200.200.200.136/api/SessionService/Sessions

payload="{“UserName”: “system”,“Password”: “system”}"

headers = {
‘Accept’: ‘application/json’,
‘Content-Type’: ‘application/json’,
}

response = requests.request(“POST”, urltoken, verify=False, headers=headers, data=payload)
print(’\b’)
print(‘Generate Token:’)
print(urltoken)
print(’\b’)

print(‘Authentication Status:?’ + ‘\b’)
print(response.content)

print(’\b’)
mytoken = response.headers.get(‘X-Auth-Token’)
print('Token Generated: ’ + ‘\n’ + mytoken)

#variables
url = “https://200.200.200.136/api/

*************************************************************

TEST CASE: POST SlotMode

*************************************************************

uri = url + ‘Systems/Slot/4/Config/SlotMode’

POST /api/Systems/Slot/4/Config/SlotMode

payload={
‘Content-Type’: ‘application/json’,
“SlotMode”: “100G x4 In/Output (IPFIX)”
}

headers = {
‘Accept’: ‘application/json’,
‘X-Auth-Token’: mytoken
}

response = requests.request(“POST”, uri, verify=False, headers=headers, data=payload)

print(’\b’)
print(‘Test Case: POST ’ + uri)
print(’\n’)

if response.status_code == 200:
print(Fore.GREEN + ‘PASS: ’ + Style.RESET_ALL + ‘Status Code = 200 Ok’)
print(’\b’)
else:

print('FAIL: Status Code = ’ + response.status_code)

print(response.status_code)

pretty_data = json.dumps(response.json(), indent=4)
print(pretty_data)

*************************************************************

THIS WORKS

TEST CASE: GET SlotMode

*************************************************************

uri = url + ‘Systems/Slot/4/Status/SlotMode’

GET /api/Systems/Slot/4/Status/SlotMode

payload={}
headers = {
‘Accept’: ‘application/json’,
‘X-Auth-Token’: mytoken
}

response = requests.request(“GET”, uri, verify=False, headers=headers, data=payload)

print(’\b’)
print(‘Test Case: GET ’ + uri)
print(’\n’)

if response.status_code == 200:
print(’\b’)
else:

print('FAIL: Status Code = ’ + response.status_code)

print(response.status_code)

pretty_data = json.dumps(response.json(), indent=4)
print(pretty_data)

Hi @lhailey and welcome to the community!

First of all, when posting questions and asking for help it would be kind to post your code in a Gist or code block so other people can easily read it. In addition, it’s always a good idea to follow good coding conventions and write consistent code that is easy to read. Of course writing clean code is a skill and no one expects you to get it right the first time, but by putting in a bit more effort you’ll be more respecting of the time of the other Community members. For now I’ve put your code in a Gist here: code.py · GitHub, although it probably won’t run well.

Having said that, let’s try to debug your issue. You seem to be using the Python requests library to interact with an HTTP API. Judging from the error message the API does not like the fact that you’re POSTing JSON to it. Without more information there is not much we can do to help you. Try consulting or linking here to the API documentation of the API you’re trying to use. Except for some consistency issues there does not seem to be a whole lot wrong with your code.