我在gorest的POST请求中应该使用什么类型?

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

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&amp;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 (
  &quot;code.google.com/p/gorest&quot;
  &quot;net/http&quot;
  &quot;fmt&quot;
)

func main() {
  gorest.RegisterService(new(HelloService)) //Register our service                                        
  http.Handle(&quot;/&quot;,gorest.Handle())
  http.ListenAndServe(&quot;:8787&quot;,nil)
}

//Service Definition                                                                                      
type HelloService struct {
  gorest.RestService `root:&quot;/api/&quot;`
  save   gorest.EndPoint `method:&quot;POST&quot; path:&quot;/save/&quot; output:&quot;string&quot; postdata:&quot;map[string]string&quot;`
}

func(serv HelloService) Save(PostData map[string]string) {
  fmt.Println(PostData)
}

And my awesome html form:

&lt;form method=&quot;POST&quot; action=&quot;http://127.0.0.1:8787/api/save/&quot;&gt;
  key: &lt;input type=&quot;text&quot; name=&quot;key&quot; /&gt;&lt;br /&gt;  
  json: &lt;input type=&quot;text&quot; name=&quot;json&quot; /&gt;&lt;br /&gt;
  &lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;

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 &#39;k&#39; 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&amp;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:&quot;POST&quot; path:&quot;/post/&quot;  postdata:&quot;User&quot; 
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.

&lt;div&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;endpont&quot; value=&quot;http://127.0.0.1:8787/api/save/&quot; /&gt;
  key: &lt;input type=&quot;text&quot; name=&quot;key&quot; /&gt;&lt;br /&gt; &lt;!-- this should be used in your post url --&gt;
  json: &lt;input type=&quot;text&quot; name=&quot;json&quot; /&gt;&lt;br /&gt; &lt;!-- this will get sent by your javascript as the post body --&gt;
  &lt;input type=&quot;button&quot; onclick=&quot;send_using_ajax();&quot; /&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2013年5月29日 05:02:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/16801468.html
匿名

发表评论

匿名网友

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

确定