Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a gofiber POST request how can I parse the request body?

How would I read and change the values if I posted JSON data to /post route in gofiber:

{
    "name" : "John Wick"
    "email" : "[email protected]"
}
app.Post("/post", func(c *fiber.Ctx) error {
    //read the req.body here
    name := req.body.name
    return c.SendString(name)
}
like image 338
Erick Li Avatar asked Dec 17 '20 04:12

Erick Li


People also ask

How do I get request body from post request?

To retrieve the body of the POST request sent to the handler, we'll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We've then returned this name back, with a greeting message.

Does POST request have body?

A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code.

How do I add a request body in HTTP POST?

NSString *requestBody = [NSString stringWithFormat:@"mystring"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];

How do I send a post body to the server?

ENTER A POST BODY As part of a POST or PUT request, a data payload can be sent to the server in the body of the request. When you select one of those methods from the method drop-down button, the API Connector form changes to display an input field for the post body. The body contents can be any valid JSON object, for example like this:

How do I create a get and POST request in go?

In Go, we use the http package to create GET and POST requests. The package provides HTTP client and server implementations. The following example creates a simple GET request in Go. We create a GET request to the webcode.me webpage.

How to decode a request with a request body larger than that?

A request body larger than that will now result in // Decode () returning a "http: request body too large" error. r.Body = http.MaxBytesReader (w, r.Body, 1048576 ) // Setup the decoder and call the DisallowUnknownFields () method on it. // This will cause Decode () to return a "json: unknown field ..."

How do I make a POST request using API connector?

API Connector defaults to the GET method. To make a POST, PUT, or PATCH request, choose that method from the drop-down menu: As part of a POST, PUT, or PATCH request, a data payload can be sent to the server in the body of the request.


2 Answers

You can use BodyParser

app.Post("/post", func(c *fiber.Ctx) error {
    payload := struct {
        Name  string `json:"name"`
        Email string `json:"email"`
    }{}

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

    return c.JSON(payload)
}
like image 160
Gokhan Sari Avatar answered Nov 10 '22 09:11

Gokhan Sari


  1. let's say the name and email are for a user, so first you create a user struct:
type User struct {
    Name string `json: "email"`
    Email string `json: "email"`    
}
  1. In your view you can get it like this:
app.Post("/post", func(c *fiber.Ctx) error {
    user := new(User)

    if err := c.BodyParser(user); err != nil {
        fmt.Println("error = ",err)
        return c.SendStatus(200)
    }

    // getting user if no error
    fmt.Println("user = ", user)
    fmt.Println("user = ", user.Name)
    fmt.Println("user = ", user.Email)

    return c.SendString(user.Name)
}
like image 25
KAMAL Avatar answered Nov 10 '22 10:11

KAMAL