英文:
Gin, Get form-data nested object sending with postman
问题
基本上,我想使用PostFormMap()
方法来获取从Postman的form-data
发送的数据,但我得到了意外的结果。我之所以不使用raw json
是因为我还想上传一个文件。这是我的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
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"}] |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论