Skip to content

🤗 [Question]: bodyParser nested with bracket notation #3223

@adriancmiranda

Description

@adriancmiranda

Question Description

Hi everyone, hope you're doing well!

I'm trying to work with some fields from a form-data request in Go, with the following example structure:

text_prompts[0][text]: "A beautiful sunset over the ocean"
text_prompts[0][weight]: 0.7
text_prompts[1][text]: "mountain range covered in snow"
text_prompts[1][weight]: -0.5
init_image: /path/to/image.jpg

When trying to parse these values into a struct, I notice that the TextPrompts field is coming as null. I did some research and found references to other libraries in discussions here in the repository. My question is: does Fiber handle this kind of form-data request natively, or do I need to add an external library specifically for this?

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "log"

type TextPrompt struct {
	Text   string  `form:"text" json:"text"`
	Weight float64 `form:"weight" json:"weight"`
}

type Request struct {
	InitImage   string       `form:"init_image" json:"init_image"`
	TextPrompts []TextPrompt `form:"text_prompts" json:"text_prompts"`
}

func main() {
	app := fiber.New()

	app.Post("/v1/generation/:engine_id/image-to-image", func(ctx *fiber.Ctx) error {
		request := Request{}
		if err := ctx.BodyParser(&request); err != nil {
			return ctx.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
				"code":  fiber.StatusUnprocessableEntity,
				"error": err.Error(),
			})
		}
		return ctx.Status(fiber.StatusOK).JSON(fiber.Map{
			"info": request,
		})
	})

	log.Fatal(app.Listen(":3000"))
}

EDIT:

multipart/form-data

curl --request POST \
  --url 'http://0.0.0.0:3000/v1/generation/sxi/image-to-image' \
  --header 'accept: application/json; charset=utf-8' \
  --header 'content-type: multipart/form-data' \
  --form 'text_prompts[0][text]=A lighthouse on a cliff' \
  --form 'text_prompts[0][weight]=0.5' \
  --form 'text_prompts[1][text]=land, ground, dirt, grass' \
  --form 'text_prompts[1][weight]=-0.9' \
  --form init_image=static/assets/sd.jpg

application/x-www-form-urlencoded

curl --request POST \
  --url 'http://0.0.0.0:3000/v1/generation/sxi/image-to-image' \
  --header 'accept: application/json; charset=utf-8' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --form 'text_prompts[0][text]=A lighthouse on a cliff' \
  --form 'text_prompts[0][weight]=0.5' \
  --form 'text_prompts[1][text]=land, ground, dirt, grass' \
  --form 'text_prompts[1][weight]=-0.9' \
  --form init_image=static/assets/sd.jpg

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions