<p>Looking for a Go POST example, I want to send data to a PHP file via POST (lots of data)... There aren't any examples that i could find... </p>
<p>If possible include the PHP side of it?</p>
<hr/>**评论:**<br/><br/>anoland: <pre><p>This may help: <a href="http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/" rel="nofollow">http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/</a></p>
<p>One thing that PHP glosses over is that a post is actually in the http body. In Go you have to read the body then decode/unmarshal that. </p></pre>ecmdome: <pre><p><a href="https://golang.org/pkg/net/http/#Post" rel="nofollow">https://golang.org/pkg/net/http/#Post</a></p></pre>SaturnsVoid: <pre><p>Obviously i have seen this. But it does nothing but tell me its possible to do in Go. I need for information, examples to learn from. </p></pre>ecmdome: <pre><p>How is it obvious that you have seen this if you asked such a question?</p>
<p>Go is a very idiomatic language, reading the libraries are pretty self explanatory. If you would have looked at the very top of that page I sent you, you would have seen a POST example with values, but I will try to break it down yo you further.</p>
<p>You are looking to use the http.PostForm function most likely, this will make it easiest to send a post request. You can also do this by creating a NewRequest which gives more flexibility but we will stick to the simple one for now.</p>
<p>This function takes 2 arguments, a string which represents your endpoint (the php file's address), and a url.Values data type which holds all of that post data.</p>
<p>If you look at url.Values(which you can click in the go documentation) you will see that it's simple a map[string][]string</p>
<p>in response you get a reponse, which you can also click the type and get all of the information you might need about it. and you get an error.</p>
<p>Now in the response, you're interested in the Body, which is a ReadCloser type, basically a readier. We will use io/ioutil to read this very quickly.. you can also use bufio to buffer the response(if you're expecting really long responses this might be best, but it's doubtful w/ php)</p>
<p>Now knowing all of that information here is some example code that will do just this. I would have linked it in the Go Playground but it restricts socket requests(rightfully so)</p>
<pre><code>package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
url := "http://requestb.in/yfc4fbyf"
data := url.Values{
"key": {"Value"},
"id": {"123"},
}
resp, err := http.PostForm(
url,
data,
)
if err != nil {
log.Println("post err")
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ioutil err")
panic(err)
}
fmt.Println(string(body))
}
</code></pre>
<p>Now this may or may not work for your data-type of input. I recommend after fiddling a bit with something like this that you look at forming your own request with http.NewRequest, you then have full reign to make "POST","PUT","GET","PATCH" etc requests, add headers, and include more versatile data types.</p>
<p>edit: reddit kind of chopped up my code</p></pre>SaturnsVoid: <pre><p>I apologize for my earlier words. I have looked at your example and tested it but it gives me the following error;</p>
<blockquote>
<p>M:\Computer\Programming\Go\HTTP POST.go:13: url.Values undefined (type string has no field or method Values)</p>
</blockquote>
<p>From what i can tell, with my limited knowledge of Go, it should be working... But its not. I am able to understand how it should do the job though, so thank you.</p></pre>schoenobates: <pre><p>My guess is you've shadowed a net url import with a string variable called url...</p></pre>jasonrichardsmith: <pre><p>I suggest you hire a contractor.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传