Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat type should I use in my POST request for gorest?
    primarykey
    data
    text
    <p>I'm very new to Go so please forgive me if this is stupid obvious.</p> <p>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</p> <p>gotest.go:</p> <pre><code>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) } </code></pre> <p>And my awesome html form:</p> <pre><code>&lt;form method="POST" action="http://127.0.0.1:8787/api/save/"&gt; key: &lt;input type="text" name="key" /&gt;&lt;br /&gt; json: &lt;input type="text" name="json" /&gt;&lt;br /&gt; &lt;input type="submit" /&gt; &lt;/form&gt; </code></pre> <p>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:</p> <p><code>Error Unmarshalling data using application/json. Client sent incompetible data format in entity. (invalid character 'k' looking for beginning of value)</code></p> <p>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.</p> <p>If <code>map[string]string</code> with <code>string</code> it prints the following to the bash terminal where I am running this:</p> <p><code>key=arst&amp;json=%7B%27arst%27%3A%27arst%27%7D</code></p> <p>In the go rest documentation the only example of this that I could find is:</p> <pre><code>posted gorest.EndPoint method:"POST" path:"/post/" postdata:"User" func(serv HelloService) Posted(posted User) </code></pre> <p>But my attempts at creating a custom struct have also fails with the same unmarshalling error seen above.</p> <pre><code>type MyStruct struct { key,json string } </code></pre> <p>Can someone please tell me what data type I should be using? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload