Beego如何访问使用multipart/form-data头部提交的参数?

huangapple go评论97阅读模式
英文:

Beego how to access params submitted with multipart/form-data header?

问题

我遇到了以下问题:
当我向我的beego应用程序发出curl请求时,

curl http://localhost:8080/controller/path -X POST -H 'Content-Type: multipart/form-data; charset=UTF-8' -F "file=@file.csv;filename=file.csv" -F "name=first"

我想从我的控制器中访问name参数,但是当我尝试

func (c *Controller) Path() {
...
var name string
c.Ctx.Input.Bind(&name, "name")
// 或者我尝试 'name := c.GetString("name")'
...
}

结果始终是一个空字符串作为name变量。

我做错了什么?在这种情况下,我如何访问参数?请给出任何建议!

更新1
我尝试了Parse to struct方法,但没有成功...

type DataParams struct {
  Name string `form:"name"`
}

cLDP := DataParams{}
if err := c.ParseForm(&cLDP); err != nil {
    return ret, err
}

更新2
根据评论,我尝试了以下方法

c.Ctx.Input.ParseFormOrMulitForm(99999)
var name string
c.Ctx.Input.Bind(&name, "name")

更新3
尝试了所有方法后,我迷失了...

name := c.Ctx.Input.Query("name")
params := c.Ctx.Input.Params()
name2 := c.GetString("name")
c.Ctx.Input.ParseFormOrMulitForm(99999)
params2 := c.Ctx.Input.Params()
fmt.Println("Debug 2->", name)
fmt.Println("Debug 5->", name2)
fmt.Println("Debug 3->", params)
fmt.Println("Debug 4->", params2)

输出结果为

Debug 2->
Debug 3-> map[...]
Debug 4-> map[...]
Debug 5->

未检测到name参数

更新4
如果我使用c.Ctx.Input.RequestBody,不出所料,它是空的。

仍然没有成功 Beego如何访问使用multipart/form-data头部提交的参数?

英文:

I've ran into a problem as follows:
When I make a curl request to my beego app

> curl http://localhost:8080/controller/path -X POST -H 'Content-Type: multipart/form-data; charset=UTF-8' -F “file=@file.csv;filename=file.csv” -F “name=first”

I want to access name param from my controller, but when I try

> func (c *Controller) Path() {
> ...
> var name string
> c.Ctx.Input.Bind(&name, "name")
> // or I've tried 'name := c.GetString("name")'
> ...
>}

result is always an empty string as name variable.

What am I doing wrong ? How can I access params in this case ? Please any advice welcome !

Update 1
I've tried Parse to struct approach, with no luck...

type   DataParams struct {
  Name string `form:"name"`
}

	cLDP := DataParams{}
	if err := c.ParseForm(&cLDP); err != nil {
		return ret, err
	}  

Update 2
as of comment, I've tried

c.Ctx.Input.ParseFormOrMulitForm(99999)
var name string
c.Ctx.Input.Bind(&name, "name")

Update 3
after trying altogether I'm lost...

name := c.Ctx.Input.Query("name")
params := c.Ctx.Input.Params()
name2 := c.GetString("name")
c.Ctx.Input.ParseFormOrMulitForm(99999)
params2 := c.Ctx.Input.Params()
fmt.Println("Debug 2->", name)
fmt.Println("Debug 5->", name2)
fmt.Println("Debug 3->", params)
fmt.Println("Debug 4->", params2)

output is

Debug 2->
Debug 3-> map[...]
Debug 4-> map[...]
Debug 5->

no name param detected

Update 4
if I use c.Ctx.Input.RequestBody it not surprisingly is empty

And still no luck Beego如何访问使用multipart/form-data头部提交的参数?

答案1

得分: 1

将"Content-Type: multipart/form-data"更改为"enctype:multipart/form-data"。

英文:

change the "Content-Type: multipart/form-data to enctype:multipart/form-data

答案2

得分: 0

你可以使用Query方法来访问name参数的值,像这样:

name := c.Ctx.Input.Query("name")

如果你想使用Bind方法,可以这样做:

names := []string{}
if err := c.Ctx.Input.Bind(&names, "name"); err != nil {
    panic(err)
}
log.Println(name[0]) // first

但是你还需要更新你的curl命令,明确将name参数声明为一个带有索引的数组:

curl ... -F "name[0]=first"

这是因为Go在内部将表单参数值存储在字符串切片中(参见url.Values),beego也是使用这种方式,所以在beego解析表单后,它会变成url.Values{"name": []string{"first"}},因此beego无法将[]string{"first"}绑定到普通的string类型中。

英文:

You should be able to access the name parameter value using the Query method, like so:

name := c.Ctx.Input.Query("name")

If you want to use Bind you can do something like this:

names := []string{}
if err := c.Ctx.Input.Bind(&names, "name"); err != nil {
    panic(err)
}
log.Println(name[0]) // first

But you'll also have to update your curl to specifically declare the name parameter as an array with index:

curl ... -F "name[0]=first"

This is because internally Go stores a form's parameter values in a slice of strings (see url.Values) and beego is using this as well, so after beego parses the form it will look like this url.Values{"name": []string{"first"}} and so beego does not know how to bind []string{"first"} into a plain string.

huangapple
  • 本文由 发表于 2017年4月18日 20:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/43471581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定