<p>Hi All,</p>
<p>Trying to implement a very basic POST request that just does a "fmt.Println" of the data I'm sending in the Angular Form but nothing gets logged in my console when I hit "submit"</p>
<pre><code>$scope.myForm = { 'name' : 'bob', 'location' : 'TX'}
$scope.addForm = function() {
$http.post('/api/myapi', $scope.myForm).success(function(data) {
console.log(data)
})
</code></pre>
<p>Go psuedo code:</p>
<pre><code>func main() {
ws = new(restful.WebService)
ws.
Path("/api").
Consumes(restful.MIME_XML, restful.MIME_JSON).
Produces(restful.MIME_JSON, restful.MIME_JSON)
ws.Route(ws.POST("/myapi").Consumes("application/x-www-form-urlencoded").To(someFunc)
}
func someFunc(req *restful.Request, res *restful.Response) {
myData := req.Request.ParseForm()
fmt.Println(myData)
}
</code></pre>
<p>The above code just returns "<nil>"</p>
<p>I feel like jumping out of the building (throwing my laptop off first though) cause I can't figure out this simple ass POST request. Either that or go back to Node/Python/PHP</p>
<p>Edit: I tried to copy the exact code in the examples and still no luck </p>
<p><a href="https://github.com/emicklei/go-restful/blob/master/examples/restful-form-handling.go" rel="nofollow">https://github.com/emicklei/go-restful/blob/master/examples/restful-form-handling.go</a></p>
<hr/>**评论:**<br/><br/>kortemy: <pre><p>Haven't tried go-restful, to be honest, but that snippet looks very boilerplate-ish.</p>
<p>You might want to check <a href="https://github.com/gin-gonic/gin" rel="nofollow">gin</a>, I love it, its very simple, concise, and reminds of how you'd write services in Sinatra or Express.</p></pre>gonoonb1212: <pre><p>This looks amazing and familiar--I'll have to try it out, thanks.</p></pre>danielfireman: <pre><p>Although the framework used is different, I believe Nathan Leclaire went to the kernel of the this problem <a href="http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/" rel="nofollow">here</a>. Totally worth reading.</p>
<p>From his own words: "I shouldn’t be trying to parse a form at all! Cue facepalm, and guilt of hours spent solving this issue (at least I’ll know better next time). My request payload wasn’t encoded as a form, it was encoded as JSON. If I just took out the call to request.ParseForm, I probably would have usable data in request.Body that I could Demarshal. Indeed, this proved to be the case."</p></pre>gonoonb1212: <pre><p>Nevermind, got it working. I had my struct all fucked up (the lowercase variable names for the struct messed it up)</p></pre>PJay-: <pre><p>Lol I feel you, made this exact same error a week ago when I was working on an uber tight deadline. Not so fun times :(</p></pre>gonoonb1212: <pre><p>Yeah this is gonna be a pain in the ass to get used to haha. I'm so used to camelCase JavaScript, the Golang syntax is kind of messing me up but slowly getting the hang of it. </p></pre>gonoonb1212: <pre><p>This confused me more than before.</p>
<p>I was able to hardcode some random JSON object, post it and see the data on the server side.</p>
<p>However, when I use form data, it doesn't work. </p></pre>icodl: <pre><p>Angular sends application/json in the POST body
If you want application/x-www-form-urlencoded you shouldn't send a JSON request ;) with Angular</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传