【问题标题】:Sending POST request to an Endpoint results in "binding element must be a struct"向端点发送 POST 请求会导致“绑定元素必须是结构”
【发布时间】:2020-11-03 03:07:29
【问题描述】:

我有一个使用 Echo 库在 Go 中编写的端点。我正在尝试通过 python 脚本发布数据,但收到“绑定元素必须是结构”错误。

来自端点的代码,我将请求主体绑定到变量(Go lang):

// get body from request
requestBody := make(map[string]interface{})
    if err := c.Bind(&requestBody); err != nil {
        return err
    }

Python 脚本:

data = {
    "AED": {
        "USD": 0.2719
        },
    "ARS": {
        "USD": 0.0142
    }
}

 headers = {
        "Authorization": # my_jwt_token,
        "API-Key": # my api_key
    }

 requests.put(url, data=payload, headers=headers)
 
 if not response.ok:
     raise Exception("Failed to update")

 return result.status_code

错误信息:

{"logType":"application","msg":"Status: 400, Error: code=400, message=binding element must be a struct, Title: Client Error, Message: binding element must be a struct, Detail: ","action":"Error Handler","level":"error","timestamp":"2020-07-13T13:39:19-07:00"}

为什么会这样?我可以使用 Postman 发送完全相同的 Payload,它工作正常,但使用 python 请求库发送 PUT 请求不起作用。

【问题讨论】:

    标签: python go struct request put


    【解决方案1】:

    想通了。 post请求中的“data”字段应更改为“json”。

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2020-09-30
      • 2019-09-08
      • 2020-11-21
      相关资源
      最近更新 更多