Sending a PUT Request to an API in Python. Error 400 Bad request

69 Views Asked by At

I am currently working on implementing a Python script to send a PUT request to an external API. However, despite formatting my payload according to the API documentation, I am receiving a "400 Bad Request" error with the following details:

{
  "errors": {
    "": [
      "Error converting value \"...\" to type 'System.Collections.Generic.IEnumerable`1[Sma.Sp.GridControlApi.Interfaces.Request.PlantSettings.PlantGridSettingsRequest]'. Path '', line 1, position 3336."
    ],
    "requests": [
      "The requests field is required."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-3711d26de9749eb9b799b1e12771085b-deb81903e5a9837b-00"
}

This is the Python Code:

    import json
    import requests
    
    payload = [
  {
    "isDynamicActivePowerLimitEnabled": True,
    "curtailmentTargetPercent": 168,
    "rampSetting": {
      "increasingRampPercentPerSecond": 189,
      "decreasingRampPercentPerSecond": 71
    },
    "timeoutInSeconds": 131,
    "isFallbackEnabled": False,
    "requestType": "ActivePowerDefaultDynamicTargetSettings"
  },
  {
    "fixedLimitPercent": 41,
    "isFixedActivePowerLimitEnabled": True,
    "requestType": "ActivePowerFixedLimitSettings"
  },
  {
    "openLoopSetting": {
      "responseTimeInMilliseconds": 372147
    },
    "underFreqActivePowerDroops": {
      "curvePoint1": {
        "frequencyHz": 36,
        "activePowerPercentChange": 75
      },
      "curvePoint2": {
        "frequencyHz": 187,
        "activePowerPercentChange": 18
      },
      "curvePoint3": {
        "frequencyHz": 46,
        "activePowerPercentChange": 30
      }
    },
    "overFreqActivePowerDroops": {
      "curvePoint1": {
        "frequencyHz": 55,
        "activePowerPercentChange": 20
      },
      "curvePoint2": {
        "frequencyHz": 54,
        "activePowerPercentChange": 161
      },
      "curvePoint3": {
        "frequencyHz": 225,
        "activePowerPercentChange": 87
      }
    },
    "isFreqWattActive": False,
    "requestType": "ActivePowerFreqWattSettings"
  },
  {
    "operationRampPercentPerSecond": 125,
    "connectionRampPercentPerSecond": 79,
    "requestType": "ActivePowerRampSettings"
  },
  {
    "openLoopSettings": {
      "responseTimeInMilliseconds": 393587
    },
    "crvRampSettings": {
      "increasingRampPercentPerSecond": 119,
      "decreasingRampPercentPerSecond": 152
    },
    "voltWattCurvePoints": [
      {
        "nominalVoltagePercentage": 248,
        "activePowerPercentage": 207
      },
      {
        "nominalVoltagePercentage": 42,
        "activePowerPercentage": 115
      },
      {
        "nominalVoltagePercentage": 204,
        "activePowerPercentage": 255
      }
    ],
    "isVoltWattEnabled": True,
    "requestType": "ActivePowerVoltWattCurveSettings"
  },
  {
    "isEnergized": False,
    "requestType": "EnergizeSettings"
  },
  {
    "highFrequencyFrtSettings": {
      "threshold1NominalFrequencyPercent": 129,
      "threshold1DurationMillis": 246,
      "threshold2NominalFrequencyPercent": 154,
      "threshold2DurationMillis": 158
    },
    "lowFrequencyFrtSettings": {
      "threshold1NominalFrequencyPercent": 11,
      "threshold1DurationMillis": 149,
      "threshold2NominalFrequencyPercent": 185,
      "threshold2DurationMillis": 148
    },
    "requestType": "FrequencyFaultRideThroughSettings"
  },
  {
    "openLoopSetting": {
      "responseTimeInMilliseconds": 453635
    },
    "rampSetting": {
      "increasingRampPercentPerSecond": 56,
      "decreasingRampPercentPerSecond": 193
    },
    "voltVarCurvePoints": [
      {
        "isOverExcited": True,
        "nominalVoltagePercentage": 221,
        "reactivePowerPercentage": 31
      },
      {
        "isOverExcited": False,
        "nominalVoltagePercentage": 162,
        "reactivePowerPercentage": 169
      },
      {
        "isOverExcited": True,
        "nominalVoltagePercentage": 123,
        "reactivePowerPercentage": 15
      }
    ],
    "isVoltVarEnabled": False,
    "requestType": "ReactivePowerVoltVarCurveSettings"
  },
  {
    "highVoltageFrtSettings": {
      "threshold1NominalVoltagePercent": 112,
      "threshold1DurationMillis": 21,
      "threshold2NominalVoltagePercent": 165,
      "threshold2DurationMillis": 218,
      "threshold3NominalVoltagePercent": 28,
      "threshold3DurationMillis": 117
    },
    "lowVoltageFrtSettings": {
      "threshold1NominalVoltagePercent": 85,
      "threshold1DurationMillis": 242,
      "threshold2NominalVoltagePercent": 124,
      "threshold2DurationMillis": 155,
      "threshold3NominalVoltagePercent": 177,
      "threshold3DurationMillis": 22
    },
    "requestType": "VoltageFaultRideThroughSettings"
  }
]
    
    # Convert to JSON format
    json_payload = json.dumps({"requests": payload})
    

    url = "https://****/endpoint"
  
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer <your_token_here>"  # Add if necessary
    }
    
    response = requests.put(url, data=json_payload, headers=headers)
    print(response.status_code)
    print(response.text)

I have tried to format my payload correctly and set the headers as required by the API. What might be causing the issue, and how can I resolve it?

0

There are 0 best solutions below