Go-Restful frustrations

xuanbao · · 825 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi All,</p> <p>Trying to implement a very basic POST request that just does a &#34;fmt.Println&#34; of the data I&#39;m sending in the Angular Form but nothing gets logged in my console when I hit &#34;submit&#34;</p> <pre><code>$scope.myForm = { &#39;name&#39; : &#39;bob&#39;, &#39;location&#39; : &#39;TX&#39;} $scope.addForm = function() { $http.post(&#39;/api/myapi&#39;, $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(&#34;/api&#34;). Consumes(restful.MIME_XML, restful.MIME_JSON). Produces(restful.MIME_JSON, restful.MIME_JSON) ws.Route(ws.POST(&#34;/myapi&#34;).Consumes(&#34;application/x-www-form-urlencoded&#34;).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 &#34;&lt;nil&gt;&#34;</p> <p>I feel like jumping out of the building (throwing my laptop off first though) cause I can&#39;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&#39;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&#39;d write services in Sinatra or Express.</p></pre>gonoonb1212: <pre><p>This looks amazing and familiar--I&#39;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: &#34;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.&#34;</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&#39;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&#39;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&#39;t send a JSON request ;) with Angular</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

825 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传