Table of Contents
Question content
Solution
Home Backend Development Golang Golang GIN error when binding form data

Golang GIN error when binding form data

Feb 10, 2024 pm 01:00 PM
stack overflow

绑定表单数据时Golang GIN出错

php editor Xinyi brings you a solution to the problem of errors when binding form data in the Golang GIN framework. When using the GIN framework for form data binding, you sometimes encounter some problems, such as the inability to parse the form data correctly. These problems may be caused by parameter binding, data type mismatch, etc. This article will introduce how to correctly bind form data and solve common errors to help developers successfully use the GIN framework for development work.

Question content

When I try to bind a form data request to a structure, it errors out with "Fatal Error: Stack Overflow".

This is my code. There's nothing to explain. I'm getting started with the code but can't figure it out.

Structure

type Wish struct {
    ID                int                `gorm:"primarykey;autoIncrement" json:"id"`
    CreatedAt         time.Time          `json:"created_at"`
    UpdatedAt         time.Time          `json:"updated_at"`
    DeletedAt         gorm.DeletedAt     `gorm:"index" json:"deleted_at"`
    UserID            int                `json:"user_id" form:"user_id"`
    User              *User              `gorm:"foreignKey:UserID" json:"user_data,omitempty"`
    WishTypeID        int                `json:"wish_type_id" form:"wish_type_id"`
    WishType          *WishType          `gorm:"foreignKey:WishTypeID" json:"wish_type_data,omitempty"`
    ProcessTrack      []*ProcessTrack    `gorm:"foreignKey:WishID" json:"process_track,omitempty"`
    VacationDateRange *VacationDateRange `gorm:"foreignKey:WishID" json:"vacation_date_range,omitempty"`
    Content           string             `gorm:"type:varchar(255)" json:"content" form:"content"`
    Status            WishStatus         `gorm:"type:integer" json:"status" form:"status"`
    Files             []*File            `gorm:"polymorphic:Module;polymorphicValue:wish_files" json:"files,omitempty"`
}

Copy after login

Controller

var wish migrations.Wish
    if err := c.Bind(&wish); err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Talep Okunamadı!"})
        return
    }
    c.JSON(200, wish)
    return
Copy after login

Request

Solution

I modified the controller

type Req struct {
        Content           string                        `form:"content"`
        WishTypeID        int                           `form:"wish_type_id"`
        VacationDateRange *migrations.VacationDateRange `form:"vacation_date_range"`
    }
    err, i, g := authorizer.AuthorizeIt(c, a.Subject, a.Action)
    if err != nil {
        c.JSON(i, g)
        return
    }
    var wishReq Req
    var wish migrations.Wish
    if err := c.Bind(&wishReq); err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Wish can't bind."})
        return
    }
    wish.WishTypeID = wishReq.WishTypeID
    wish.Content = wishReq.Content
    wish.VacationDateRange = wishReq.VacationDateRange
Copy after login

But I still don't understand why it can't be the first style. I've also added common usage. It usually works too.

err, i, g := authorizer.AuthorizeIt(c, a.Subject, a.Action)
    if err != nil {
        c.JSON(i, g)
        return
    }
    var announce mig.Announce

    err = c.Bind(&announce)
    if err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Announce can't bind. Error Code: AN-CRT-20"})
        return
    }
Copy after login

The above is the detailed content of Golang GIN error when binding form data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Spring Security 6: cors() is deprecated and marked for removal Spring Security 6: cors() is deprecated and marked for removal Feb 10, 2024 pm 11:45 PM

I have the following code: publicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{returnhttp.httpBasic().disable().cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

Golang+CGO using ucontext crashes (intentionally) with SIGSEGV or SIGTRAP when using different stacks Golang+CGO using ucontext crashes (intentionally) with SIGSEGV or SIGTRAP when using different stacks Feb 09, 2024 pm 12:15 PM

I'm currently writing a Golang+CGO program and will be using posixucontext in CGO. Since all my core logic will be in ucontext's bind function, we should catch all errors in the code. I tested it by accessing a null pointer, which gave me completely different behavior, all depending on the stack location used by the ucontext. Below are more details with simplified examples. If I allocate the ucontext stack on the thread's stack, it triggers SIGSEGV. But if I allocate it on the heap, it triggers SIGSEGV first and then SIGT when morestack_noctxt is called

Why does c++ crash when it starts executing? Why does c++ crash when it starts executing? Apr 22, 2024 pm 05:57 PM

Reasons for a C++ program to crash when starting include: missing required libraries or dependencies, uninitialized pointers or reference stack overflows, segfaults, operating system configuration issues, program errors, hardware issues

How to solve C++ runtime error: 'stack overflow'? How to solve C++ runtime error: 'stack overflow'? Aug 25, 2023 pm 10:00 PM

How to solve the C++ runtime error: 'stackoverflow' In a C++ program, when the recursion level is too deep or the memory used by the program exceeds the stack capacity, a runtime error "stackoverflow" will occur. When this error occurs, the program crashes, and it is difficult to identify the specific cause. This article will introduce some ways to solve 'stackoverflow' errors and provide some code examples. The main cause of the runtime error "stackoverflow" is that within the stack

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

What impact do C++ functions have on program performance? What impact do C++ functions have on program performance? Apr 12, 2024 am 09:39 AM

The impact of functions on C++ program performance includes function call overhead, local variable and object allocation overhead: Function call overhead: including stack frame allocation, parameter transfer and control transfer, which has a significant impact on small functions. Local variable and object allocation overhead: A large number of local variable or object creation and destruction can cause stack overflow and performance degradation.

What is the difference between Java functions and Haskell functions? What is the difference between Java functions and Haskell functions? Apr 23, 2024 pm 09:18 PM

The main difference between Java and Haskell functions is: Syntax: Java uses the return keyword to return results, while Haskell uses the assignment symbol (=). Execution model: Java uses sequential execution, while Haskell uses lazy evaluation. Type system: Java has a static type system, while Haskell has a powerful flexible type system that checks types at compile time and run time. Practical performance: Haskell is more efficient than Java when handling large inputs because it uses tail recursion, while Java uses recursion.

See all articles