Gin,使用Postman发送嵌套对象的表单数据。

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

Gin, Get form-data nested object sending with postman

问题

基本上,我想使用PostFormMap()方法来获取从Postman的form-data发送的数据,但我得到了意外的结果。我之所以不使用raw json是因为我还想上传一个文件。这是我的Postman截图:

Gin,使用Postman发送嵌套对象的表单数据。

这是我的代码(没有什么特别的):

c.JSON(200, gin.H{
    "request": c.PostFormMap("rules"),
})

我期望得到的结果是:

{
    "request": [
        {
            "cell": "A",
            "rule": "B"
        },
        {
            "cell": "B",
            "rule": "D"
        }
    ]
}

但实际上我得到的是:

{
    "request": {
        "0": "B",
        "1": "D"
    }
}

问题是如何获得正确的结果?

英文:

So basically I want to use PostFormMap() method to get my data sent from postman's form-data but I get unexpected result and the reason that I'm not using raw json is that I want to upload a file too
Here is my postman

Gin,使用Postman发送嵌套对象的表单数据。

here is the code (nothing fancy)

c.JSON(200, gin.H{
	"request": c.PostFormMap("rules"),
})

and I'm expecting to get

{
    "request": [
        {
            "cell": "A",
            "rule": "B"
        },
        {
            "cell": "B",
            "rule": "D"
        }
    ]
}

but instead I get this

{
    "request": {
        "0": "B",
        "1": "D"
    }
}

The question is how can I get the correct result?

答案1

得分: 1

回答我的问题
在经过几个小时的搜索和考虑到我对PHP的背景后,我意识到在PHP中手动实现像foo[0][bar]这样的form-data参数并不是一个标准的事情,所以在我的情况下,我决定将第二个参数作为原始的JSON发送。
所以它应该是这样的:

Postman Body => form-data

文件 sample.xlsx
规则 [{"cell": "A", "rules": "string"}, {"cell": "B", "rules": "numeric"}]
英文:
Answering my own question

After spending hours of searching and having PHP background in my mind I realized that having such form-data parameter like foo[0][bar] manually implemented in PHP and is not a standard thing so in my case I decided to send the second parameter as a raw json
so it would be

Postman Body => form-data

Key Value
file sample.xlsx
rules [{"cell": "A", "rules": "string"}, {"cell": "B", "rules": "numeric"}]

huangapple
  • 本文由 发表于 2022年4月11日 22:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/71829679.html
匿名

发表评论

匿名网友

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

确定