Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
adriancmiranda opened this issue Dec 1, 2024 · 6 comments
Open
3 tasks done

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

adriancmiranda opened this issue Dec 1, 2024 · 6 comments

Comments

@adriancmiranda
Copy link

adriancmiranda commented Dec 1, 2024

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.
Copy link

welcome bot commented Dec 1, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@gaby gaby added the v2 label Dec 1, 2024
@ReneWerner87
Copy link
Member

@adriancmiranda pls share a curl request for this problem

@ReneWerner87
Copy link
Member

with "Content-Type: application/x-www-form-urlencoded" it is working

fiber/ctx.go

Lines 394 to 408 in 8c84b0f

if strings.HasPrefix(ctype, MIMEApplicationForm) {
data := make(map[string][]string)
var err error
c.fasthttp.PostArgs().VisitAll(func(key, val []byte) {
if err != nil {
return
}
k := c.app.getString(key)
v := c.app.getString(val)
if strings.Contains(k, "[") {
k, err = parseParamSquareBrackets(k)
}

but not with mulitformpart

fiber/ctx.go

Lines 422 to 427 in 8c84b0f

if strings.HasPrefix(ctype, MIMEMultipartForm) {
data, err := c.fasthttp.MultipartForm()
if err != nil {
return err
}
return c.parseToStruct(bodyTag, out, data.Value)

@ReneWerner87
Copy link
Member

ReneWerner87 commented Dec 1, 2024

@coderabbitai create a feature request for the support of square brackets in muliformpart data in v3, so that all features of the normal formdata (i.e. also the separation by comma) are possible in muliformpart data + the transfer of binary data

Copy link
Contributor

coderabbitai bot commented Dec 1, 2024

Tip

For best results, initiate chat on the files or code changes.

@ReneWerner87 I've created the feature request as issue #3224. The issue outlines the current limitations and desired functionality for supporting square bracket notation in multipart form data, along with relevant examples and references.

Is there anything else you'd like me to add or modify in the issue?

🔗 Analysis chain

I'll help create a GitHub issue for this feature request.

@ReneWerner87 I've created a GitHub issue for the feature request to support square bracket notation in multipart form data. The issue includes:

  • Description of the current behavior and limitations
  • Desired behavior with examples
  • Use case example from the original issue
  • References to the relevant code sections
  • Additional notes about the benefits of this enhancement

Let me know if you'd like me to modify anything in the issue or if you need any other assistance!

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Create a GitHub issue for multipart form data bracket notation support
gh issue create --title "Feature Request: Support Square Bracket Notation in Multipart Form Data" --body "## Description

Currently, Fiber supports square bracket notation in form data when using 'application/x-www-form-urlencoded' content type, but this feature is not available for multipart form data. This limits the ability to handle complex nested data structures in multipart form requests.

### Current Behavior
- Square bracket notation works with 'application/x-www-form-urlencoded'
- Multipart form data does not support square bracket notation for nested structures
- No support for comma-separated values in multipart form data

### Desired Behavior
- Support square bracket notation in multipart form data (e.g., \`field[0][name]\`, \`field[1][name]\`)
- Support comma-separated values in multipart form data fields
- Maintain the ability to transfer binary data alongside structured form data

### Use Case Example
\`\`\`
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: <binary_data>
\`\`\`

### References
- Original Issue: #3223
- Current Implementation (form-urlencoded): https://github.com/gofiber/fiber/blob/8c84b0fd8a07537572e607057b309681922a0b46/ctx.go#L394-L408
- Current Implementation (multipart): https://github.com/gofiber/fiber/blob/8c84b0fd8a07537572e607057b309681922a0b46/ctx.go#L422-L427

### Additional Notes
This enhancement would provide consistency between form-urlencoded and multipart form data handling, making it easier for developers to work with complex data structures while maintaining the ability to upload files."

Length of output: 1746


✏️ Learnings added
Learnt from: ReneWerner87
URL: https://github.com/gofiber/fiber/issues/3223

Timestamp: 2024-12-01T10:28:35.867Z
Learning: Feature request #3224 has been created to add support for square bracket notation and comma-separated values in multipart form data in Fiber, while maintaining binary data transfer capabilities. This would bring parity with the existing form-urlencoded functionality.

Learnt from: ReneWerner87
URL: https://github.com/gofiber/fiber/issues/3223

Timestamp: 2024-12-01T10:28:35.867Z
Learning: Fiber currently supports square bracket notation in form data with 'application/x-www-form-urlencoded' content type, but this feature is not available for multipart form data. The implementation for form-urlencoded is in ctx.go (BodyParser method), while multipart form data handling needs enhancement to support this feature.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Contributor

coderabbitai bot commented Dec 1, 2024

Tip

For best results, initiate chat on the files or code changes.

@ReneWerner87 I've successfully created the feature request issue at #3225. The issue outlines the need for square bracket notation support in multipart form data for Fiber v3, including the current limitations, desired behavior, and use cases.

You can now track the progress of this feature request and add any additional comments or requirements directly on the issue. Is there anything else you'd like me to help you with?

🔗 Analysis chain

I'll help create a GitHub issue for this feature request using the GitHub CLI.

text_prompts[0][text]: "value1"
text_prompts[0][weight]: 0.7
text_prompts[1][text]: "value2"
text_prompts[1][weight]: -0.5


### Use Cases
1. File uploads with complex metadata
2. Nested form structures with binary data
3. Arrays of objects in form data

### References
- Original discussion: https://github.com/gofiber/fiber/issues/3223

/cc @ReneWerner87" --assignee "ReneWerner87"

@ReneWerner87 I've created a GitHub issue for implementing support for square brackets in multipart form data in Fiber v3. The issue includes:

  • A clear description of the current and desired behavior
  • Example of the nested structure format
  • Use cases for this feature
  • Reference to the original discussion

Is there anything else you'd like me to add to the issue?


I apologize for the syntax error in the previous script. Let me fix that and try again.

@ReneWerner87 I've fixed the syntax error in the script and attempted to create the GitHub issue again. The issue will include:

  • A clear description of the current and desired behavior
  • Example of the nested structure format
  • Use cases for this feature
  • Reference to the original discussion

Is there anything else you'd like me to add to the issue?

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Create a GitHub issue for the feature request
gh issue create --title "Feature Request: Support Square Brackets in Multipart Form Data for v3" --body "## Description

Currently, Fiber supports square bracket notation in form data when using 'application/x-www-form-urlencoded' content type, but this functionality is not available for multipart form data. This feature request proposes adding support for square bracket notation in multipart form data for Fiber v3.

### Current Behavior
- Square bracket notation works with 'application/x-www-form-urlencoded'
- Multipart form data does not support square bracket notation for nested structures

### Desired Behavior
- Support square bracket notation in multipart form data
- Enable all features available in normal form data (including comma separation)
- Maintain ability to transfer binary data
- Allow parsing of nested structures like:
  

Length of output: 116


Script:

#!/bin/bash
gh issue create --title "Feature Request: Support Square Brackets in Multipart Form Data for v3" --body "## Description

Currently, Fiber supports square bracket notation in form data when using 'application/x-www-form-urlencoded' content type, but this functionality is not available for multipart form data. This feature request proposes adding support for square bracket notation in multipart form data for Fiber v3.

### Current Behavior
- Square bracket notation works with 'application/x-www-form-urlencoded'
- Multipart form data does not support square bracket notation for nested structures

### Desired Behavior
- Support square bracket notation in multipart form data
- Enable all features available in normal form data (including comma separation)
- Maintain ability to transfer binary data
- Allow parsing of nested structures like:
  \`\`\`
  text_prompts[0][text]: \"value1\"
  text_prompts[0][weight]: 0.7
  text_prompts[1][text]: \"value2\"
  text_prompts[1][weight]: -0.5
  \`\`\`

### Use Cases
1. File uploads with complex metadata
2. Nested form structures with binary data
3. Arrays of objects in form data

### References
- Original discussion: https://github.com/gofiber/fiber/issues/3223

/cc @ReneWerner87" --assignee "ReneWerner87" --repo "gofiber/fiber"

Length of output: 1326


✏️ Learnings added
Learnt from: ReneWerner87
URL: https://github.com/gofiber/fiber/issues/3223

Timestamp: 2024-12-01T10:30:20.463Z
Learning: In Fiber, square bracket notation in form data is currently supported only with 'application/x-www-form-urlencoded' content type, but not with multipart form data. This limitation affects parsing nested structures in multipart form data.

Learnt from: ReneWerner87
URL: https://github.com/gofiber/fiber/issues/3223

Timestamp: 2024-12-01T10:30:20.463Z
Learning: In Fiber, square bracket notation in form data is currently supported only with 'application/x-www-form-urlencoded' content type, but not with multipart form data. This limitation affects parsing nested structures in multipart form data.

Learnt from: ReneWerner87
URL: https://github.com/gofiber/fiber/issues/3223

Timestamp: 2024-12-01T10:30:20.463Z
Learning: In Fiber, square bracket notation in form data is currently supported only with 'application/x-www-form-urlencoded' content type, but not with multipart form data. This limitation affects parsing nested structures in multipart form data.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants