Go: 使用负载/请求体进行POST请求

huangapple go评论115阅读模式
英文:

Go: Posting with Payload/Post Body

问题

尝试在Go中进行HTTP Post请求:

请求地址:apiUrl

请求体(期望为JSON字符串):postBody

这是我得到的错误信息:

  1. 无法将postBodyJson(类型为[]byte)作为http.Post的参数类型io.Reader使用:
  2. []byte类型没有实现io.Reader(缺少Read方法)

我做错了什么?

代码:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. func main() {
  8. var postBody = []string{
  9. "http://google.com",
  10. "http://facebook.com",
  11. "http://youtube.com",
  12. "http://yahoo.com",
  13. "http://twitter.com",
  14. "http://live.com",
  15. }
  16. requestUrl := "http://lsapi.seomoz.com/linkscape/url-metrics"
  17. postBodyJson, _ := json.Marshal(postBody)
  18. resp, err := http.Post(requestUrl, "application/json", postBodyJson)
  19. fmt.Println(resp)
  20. }
英文:

Trying to accomplish a HTTP Post in Go:

Posting to: apiUrl

Payload/Post Body (expected as a json string): postBody

Here is the error I'm getting:

  1. cannot use postBodyJson (type []byte) as type io.Reader in argument to http.Post:
  2. []byte does not implement io.Reader (missing Read method)

What am I doing wrong?

Code:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. func main() {
  8. var postBody = []string{
  9. "http://google.com",
  10. "http://facebook.com",
  11. "http://youtube.com",
  12. "http://yahoo.com",
  13. "http://twitter.com",
  14. "http://live.com",
  15. }
  16. requestUrl := "http://lsapi.seomoz.com/linkscape/url-metrics"
  17. postBodyJson, _ := json.Marshal(postBody)
  18. resp, err := http.Post(requestUrl, "application/json", postBodyJson)
  19. fmt.Println(resp)
  20. }

答案1

得分: 3

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. )
  9. func main() {
  10. var postBody = []string{
  11. "http://google.com",
  12. "http://facebook.com",
  13. "http://youtube.com",
  14. "http://yahoo.com",
  15. "http://twitter.com",
  16. "http://live.com",
  17. }
  18. apiUrl := "http://lsapi.seomoz.com/linkscape/url-metrics"
  19. buf := bytes.NewBuffer(nil)
  20. enc := json.NewEncoder(buf)
  21. err := enc.Encode(postBody)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. resp, err := http.Post(apiUrl, "application/json", buf)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. fmt.Println(resp)
  30. }

类似这样的代码可能会起作用。但正如我在评论中已经提到的,你应该更加熟悉这门语言。在发布代码时,请确保它可以编译。

英文:
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. )
  9. func main() {
  10. var postBody = []string{
  11. "http://google.com",
  12. "http://facebook.com",
  13. "http://youtube.com",
  14. "http://yahoo.com",
  15. "http://twitter.com",
  16. "http://live.com",
  17. }
  18. apiUrl := "http://lsapi.seomoz.com/linkscape/url-metrics"
  19. buf := bytes.NewBuffer(nil)
  20. enc := json.NewEncoder(buf)
  21. err := enc.Encode(postBody)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. resp, err := http.Post(apiUrl, "application/json", buf)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. fmt.Println(resp)
  30. }

Something like this might work. But as I already said in the comments, you should familiarize yourself with the language a little more. When you post code, make sure it compiles.

huangapple
  • 本文由 发表于 2014年7月11日 12:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/24689914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定