英文:
XML HTTP Post in Go
问题
我正在尝试创建一个使用XML的POST请求。
我知道我需要使用:
http.Post("myurl", "text/xml", body)
但我无法弄清楚如何设置要发送的实际XML数据。
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>
并设置一个头部,使其具有Content-Type: text/xml
。
英文:
I'm attempting to create a post request that uses xml.
I know I need to use:
http.Post("myurl", "text/xml", body)
But the thing I can't figure out is how to set the actual XML data to be sent.
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>
And setting a header to have Content-Type: text/xml
答案1
得分: 12
不需要自己发起请求或手动设置内容类型,可以通过一个简单的http.Post
调用完成所有操作:
package main
import (
"log"
"net/http"
"strings"
)
func main() {
const myurl = "http://127.0.0.1:8080"
const xmlbody = `
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>`
resp, err := http.Post(myurl, "text/xml", strings.NewReader(xmlbody))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// 对resp.Body进行处理
}
无论你使用什么作为bodyType
参数,都会放入内容类型头中。
服务器将看到:
POST / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Go 1.1 package http
Content-Length: 127
Content-Type: text/xml
Accept-Encoding: gzip
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>
(额外的空行是因为我选择在新的一行上开始字符串文字)。
英文:
No need to make your own request or manually set the content type, it can all be done with a single simple http.Post
call:
package main
import (
"log"
"net/http"
"strings"
)
func main() {
const myurl = "http://127.0.0.1:8080"
const xmlbody = `
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>`
resp, err := http.Post(myurl, "text/xml", strings.NewReader(xmlbody))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Do something with resp.Body
}
Whatever you use for the bodyType
argument goes into the content type header.
The server will see:
POST / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Go 1.1 package http
Content-Length: 127
Content-Type: text/xml
Accept-Encoding: gzip
<request>
<parameters>
<email>test@test.com</email>
<password>test</password>
</parameters>
</request>
(The extra blank line is because I chose to start the string literal on a new line).
答案2
得分: 8
我也是刚接触Go语言,但我会尽力回答你的问题。
你可以使用http.Client
和http.NewRequest
,然后在发送POST请求之前设置请求头。示例如下:
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
body := "<request> <parameters> <email>test@test.com</email> <password>test</password> </parameters> </request>"
client := &http.Client{}
req, err := http.NewRequest("POST", "http://localhost:8080/", bytes.NewBuffer([]byte(body)))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp)
}
客户端的响应(虚拟服务器,所以响应肯定不好):
>>> go run xmlpost.go
Post http://localhost:8080/: dial tcp 127.0.0.1:8080: connection refused
<nil>
服务器的响应:
>>> ncat -l 8080
POST / HTTP/1.1
Host: localhost:8080
User-Agent: Go 1.1 package http
Content-Length: 102
Content-Type: application/xml; charset=utf-8
Accept-Encoding: gzip
<request> <parameters> <email>test@test.com</email> <password>test</password> </parameters> </request>
更新:感谢@DaveC指出不必要的io.Reader(...)
,并为在SO中使用**#**作为注释表示道歉。现在已经修复。
英文:
I am also new to Go, but I'd take a brave trial and try to answer this.
You can use http.Client
with http.NewRequest
, then set the request header before sending the POST, working sample like this:
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
// or you can use []byte(`...`) and convert to Buffer later on
body := "<request> <parameters> <email>test@test.com</email> <password>test</password> </parameters> </request>"
client := &http.Client{}
// build a new request, but not doing the POST yet
req, err := http.NewRequest("POST", "http://localhost:8080/", bytes.NewBuffer([]byte(body)))
if err != nil {
fmt.Println(err)
}
// you can then set the Header here
// I think the content-type should be "application/xml" like json...
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
// now POST it
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp)
}
Response from Client (dummy server so bad response of course):
>>> go run xmlpost.go
Post http://localhost:8080/: dial tcp 127.0.0.1:8080: connection refused
<nil>
Server Response:
>>> ncat -l 8080
POST / HTTP/1.1
Host: localhost:8080
User-Agent: Go 1.1 package http
Content-Length: 102
Content-Type: application/xml; charset=utf-8
Accept-Encoding: gzip
<request> <parameters> <email>test@test.com</email> <password>test</password> </parameters> </request>
update:
Thanks for @DaveC pointing out the unnecessary io.Reader(...), and apologized for using # for comment, I'm too used to Python and it came natural when adding comment in SO. Now fixed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论