简体   繁体   English

将 rest api POST 请求从 json 转换为 golang 中的表单数据

[英]convert rest api POST request from json to form-data in golang

I have the following code that works for JSON in the body of the POST request but i now want to convert this to using form-data in the body of the request我有以下代码适用于 POST 请求正文中的JSON但我现在想将其转换为在请求正文中使用form-data

Here is what i have这是我所拥有的

func Signin(c *fiber.Ctx) error {
    var data map[string]string

    if err := c.BodyParser(&data); err != nil {
        return err
    }

    var user models.User

    token, err := middlewares.GenerateJWT(user.Email)

    if err != nil {
        c.Status(fiber.StatusBadRequest)
        return c.JSON(fiber.Map{
            "message": "Invalid credentials",
        })  
    }

    cookie := fiber.Cookie{
        Name: "access_token",
        Value: token,
        Expires: time.Now().Add(time.Hour * 24),
        HTTPOnly: true,
        Secure:   true,
    }

    c.Cookie(&cookie)

    return c.JSON(fiber.Map{
        "access_token": token,
        "token_type": "bearer",
    })

}

above works fine for raw JSON body but i want to change to form-data body以上适用于原始JSON主体,但我想更改为form-data主体

I have tried many things including this but to no avail我已经尝试了很多事情,包括这个但无济于事

type SigninData struct {
    email  string `json:"email" xml:"email" form:"email"`
    password string `json:"password" xml:"password" form:"password"`
}


func Signin(c *fiber.Ctx) error {
    data := new(SigninData)

    if err := c.BodyParser(&data); err != nil {
        return err
    }

    var user models.User

    token, err := middlewares.GenerateJWT(user.Email)

    if err != nil {
        c.Status(fiber.StatusBadRequest)
        return c.JSON(fiber.Map{
            "message": "Invalid credentials",
        })  
    }


    cookie := fiber.Cookie{
        Name: "access_token",
        Value: token,
        Expires: time.Now().Add(time.Hour * 24),
        HTTPOnly: true,
        Secure:   true,
    }

    c.Cookie(&cookie)

    return c.JSON(fiber.Map{
        "access_token": token,
        "token_type": "bearer",
    })

}

but i get the following error但我收到以下错误

schema: interface must be a pointer to struct

what am i missing that i need to fix to get this to accept form-data?我错过了什么,我需要修复它才能接受表单数据?

The method BodyParser expects a pointer to a struct as an argument, but your code is trying to pass it a pointer to a pointer to a struct. BodyParser方法需要一个指向结构的指针作为参数,但您的代码试图将一个指向结构指针的指针传递给它。 Please initialize the struct this way:请以这种方式初始化结构:

data := SigninData{}

Also, try to make the fields of the SigninData struct public:另外,尝试公开SigninData结构的字段:

type SigninData struct {
    Email  string `json:"email" xml:"email" form:"email"`
    Password string `json:"password" xml:"password" form:"password"`
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 API网关映射模板:将表单数据(POST)请求转换为JSON - API Gateway mapping template : Convert form-data(POST) request to JSON 如何在Rest API中处理多部分表单数据请求 - How to handle multiipart form-data request in Rest API 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios 将json post请求更改为multipart / form-data - Change a json post request to multipart/form-data 改造表单数据POST不返回JSON - Retrofit form-data POST not return JSON 是否可以在 POST 请求正文中发送 Json 数据和图像作为表单数据 - Is it possible to send Json Data in the POST request body and an image as form-data soapUI:具有文件附件和json对象的multipart / form-data REST请求 - soapUI: multipart/form-data REST request with a file attachment and json object 邮递员:通过表单数据的嵌套 JSON 的 POST 请求不起作用(而通过原始数据可以) - Postman: POST request of nested JSON via form-data not working (while via raw-data ok) 邮递员在尝试使用表单数据执行 POST 时返回 415,但与 JSON 相同的请求工作正常 - Postman returns 415 when trying to do a POST using form-data, but the same request with JSON works fine 从 ExtJS 表单到.Net5 controller 的多部分/表单数据的 POST 请求得到 responseText 为空 - POST-request with multipart/form-data from ExtJS form to .Net5 controller gets responseText empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM