英文:
What type should I use in my POST request for gorest?
问题
我对Go语言非常陌生,所以如果我说的很明显,请原谅我。
我正在尝试使用gorest将一个表单发布到用Go编写的REST API。我已经成功地使用GET完成了这个操作,但是我无法将POST数据解析为一个map。这是我的Go代码
gotest.go:
package main
import (
"code.google.com/p/gorest"
"net/http"
"fmt"
)
func main() {
gorest.RegisterService(new(HelloService)) //注册我们的服务
http.Handle("/",gorest.Handle())
http.ListenAndServe(":8787",nil)
}
//服务定义
type HelloService struct {
gorest.RestService `root:"/api/"`
save gorest.EndPoint `method:"POST" path:"/save/" output:"string" postdata:"map[string]string"`
}
func(serv HelloService) Save(PostData map[string]string) {
fmt.Println(PostData)
}
和我的精彩的HTML表单:
<form method="POST" action="http://127.0.0.1:8787/api/save/">
key: <input type="text" name="key" /><br />
json: <input type="text" name="json" /><br />
<input type="submit" />
</form>
我认为这将把我的POST数据转换为一个我可以访问的好的map。我填写表单,点击提交,然后返回一个错误:
Error Unmarshalling data using application/json. Client sent incompetible data format in entity. (invalid character 'k' looking for beginning of value)
编辑:正如greggory.hz指出的那样,程序似乎认为POST数据是一个json。这个错误是因为json必须以大括号、方括号或引号开头。
如果map[string]string
中的string
,它会将以下内容打印到我运行这个程序的终端:
key=arst&json=%7B%27arst%27%3A%27arst%27%7D
在go rest文档中,我找到的唯一一个例子是:
posted gorest.EndPoint method:"POST" path:"/post/" postdata:"User"
func(serv HelloService) Posted(posted User)
但是,我尝试创建一个自定义的结构体也失败了,出现了上面看到的相同的解组错误。
type MyStruct struct {
key,json string
}
有人能告诉我应该使用什么数据类型吗?
英文:
I'm very new to Go so please forgive me if this is stupid obvious.
I'm trying to post a form to a REST API written in Go using gorest. I've successfully done this using GET, but I can't get the POST data to parse into a map. Here is my Go code
gotest.go:
package main
import (
"code.google.com/p/gorest"
"net/http"
"fmt"
)
func main() {
gorest.RegisterService(new(HelloService)) //Register our service
http.Handle("/",gorest.Handle())
http.ListenAndServe(":8787",nil)
}
//Service Definition
type HelloService struct {
gorest.RestService `root:"/api/"`
save gorest.EndPoint `method:"POST" path:"/save/" output:"string" postdata:"map[string]string"`
}
func(serv HelloService) Save(PostData map[string]string) {
fmt.Println(PostData)
}
And my awesome html form:
<form method="POST" action="http://127.0.0.1:8787/api/save/">
key: <input type="text" name="key" /><br />
json: <input type="text" name="json" /><br />
<input type="submit" />
</form>
I would think that would turn my post data into a nice map that I could access. I fill out the form, hit submit, and it returns an error:
Error Unmarshalling data using application/json. Client sent incompetible data format in entity. (invalid character 'k' looking for beginning of value)
EDIT: As greggory.hz points out, the program seems to think that the post data is a json. This error is because a json has to start with a brace, bracket or quote.
If map[string]string
with string
it prints the following to the bash terminal where I am running this:
key=arst&json=%7B%27arst%27%3A%27arst%27%7D
In the go rest documentation the only example of this that I could find is:
posted gorest.EndPoint method:"POST" path:"/post/" postdata:"User"
func(serv HelloService) Posted(posted User)
But my attempts at creating a custom struct have also fails with the same unmarshalling error seen above.
type MyStruct struct {
key,json string
}
Can someone please tell me what data type I should be using?
答案1
得分: 3
你正在尝试将一个HTML表单发布到一个期望JSON主体的服务。但是你的浏览器不会将发布格式化为application/json,而是将其格式化为urlencoded主体。问题不在于你的Go服务器代码,而是在于HTML表单。你可能希望使用JavaScript来打包和发送你的发布,而不是使用标准的HTML表单。
<div>
<input type="hidden" name="endpont" value="http://127.0.0.1:8787/api/save/" />
key: <input type="text" name="key" /><br /> <!-- 这应该在你的发布URL中使用 -->
json: <input type="text" name="json" /><br /> <!-- 这将由你的JavaScript作为发布主体发送 -->
<input type="button" onclick="send_using_ajax();" />
</div>
英文:
You are trying to post an html form to a service expecting a json body. But your browser isn't going to format the post as application/json. It will instead format it as a urlencoded body. The problem isn't in your go server code it's in the html form. You probably want to use javascript to package up and send your post instead of a standard html form.
<div>
<input type="hidden" name="endpont" value="http://127.0.0.1:8787/api/save/" />
key: <input type="text" name="key" /><br /> <!-- this should be used in your post url -->
json: <input type="text" name="json" /><br /> <!-- this will get sent by your javascript as the post body -->
<input type="button" onclick="send_using_ajax();" />
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论