Home > Back-end >  API is saying that fields are required although I think the fields are being provided: Python/JSON
API is saying that fields are required although I think the fields are being provided: Python/JSON

Time:12-14

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?

CodePudding user response:

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?

  • Related