API is saying that fields are required although I think the fields are being provided: Python/JSON

Question:

I am working with an API of which has a few simple commands. I am trying to modify the settings of a power distribution unit through this API. I believe I must be doing something wrong with Python or JSON but cannot figure out what it is exactly. I keep getting the same error, yet I feel like I am meeting the requirements.

The code I wrote is this (some info taken out for security):

import requests
import json
url = "http://*my_ip*/api/ports/7"

payload = json.dumps({
    "status": True,
    "lastOnTime": "2022-11-02T05:29:03.756865Z",
    "type": 1
})

headers = {
    "Authorization": "JWT *my_token*",
    "Content-Type": "application/json"
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)
print(response.reason)

The response that I get from this is:
{"lastOnTime":["This field is required."],"type":["This field is required."]}
Bad Request

Doing a GET request provides this response:
{"id":7,"label":"Miner7","notes":"Notes for Miner7","status":true,"warning":true,"autoReset":true,"thresholdLow":0,"thresholdHigh":4200,"maxAttemptsLow":5,"maxAttemptsHigh":5,"attemptsLow":5,"attemptsHigh":0,"resetDelayLow":360,"resetDelayHigh":20,"timeOffLow":360,"timeOffHigh":120,"timeBeforeResetLow":360,"timeBeforeResetHigh":16,"postStateLow":true,"postStateHigh":true,"total":16607600,"lastOnTime":"2022-11-02T05:29:03.756865Z","active":true,"addTime":"2022-04-22T21:33:26.283301Z","updateTime":"2022-11-02T05:35:12.591053Z","type":1}

I am putting the fields of lastOnTime and type into the payload, but for some reason it does not seem they are being acknowledged. What am I doing wrong here?

Asked By: Lukas Grant

||

Answers:

I’m almost certain that the issue is on the api side instead of the code you provided here. Maybe you could provide code of the endpoint you are using?

Answered By: Tuomas Kangas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.