json 在Python中向API发送PUT请求,错误400 Bad request

aij0ehis  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(85)

我目前正在实现一个Python脚本来向外部API发送PUT请求。然而,尽管根据API文档格式化了我的有效负载,我还是收到了一个“400 Bad Request”错误,详细信息如下:

{
  "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"
}

字符串
下面是Python代码:

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)


我已经尝试正确格式化我的有效负载,并按照API的要求设置头。可能是什么导致了这个问题,我如何解决它?

yyyllmsg

yyyllmsg1#

400 Bad Request错误表示服务器无法理解请求。此错误可能有多种原因,例如请求参数不正确、缺少标头或请求正文格式错误。
下面是一个使用Python中的requests库发送PUT请求的基本示例:

import requests

url = "https://example.com/api/resource"
data = {"key": "value"}

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_ACCESS_TOKEN",  # Include any required headers
}

response = requests.put(url, json=data, headers=headers)

print(f"Status code: {response.status_code}")
print(f"Response content: {response.text}")

字符串
确保将占位符值替换为实际的API端点、数据和头。
如果你仍然得到一个400错误,请考虑以下几点:
1.**检查API文档:**确保使用API文档中指定的正确端点、头和请求参数。
1.**验证授权:**如果您的API需要身份验证,请确保您在Authorization头中提供了正确的凭据或访问令牌。
1.**请求载荷格式:**请确保您在请求体(示例中为json=data)中发送的数据符合预期格式。
1.**Header:**请确认您是否设置了任何必需的Header,例如Content-Type
1.**打印:**打印响应内容(response.text),查看服务器是否提供了有关错误的详细信息。
如果您能提供有关您的请求和您收到的任何错误消息的更多详细信息,我可能能够进一步帮助您。

相关问题