how to use Go newRequest method

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

how to use Go newRequest method

问题

所以,我刚开始学习Go,并尝试学习HTTP包。我在互联网上找到了这个程序:

headers := make(map[string]string)
headers["authorization"] = "1432536546" //授权字符串

url := "anUrl"

req, err := http.NewRequest("DELETE", url, headers)
if err != nil {
    panic(err)
}

但是它是错误的,因为似乎不能像这样使用映射,因为它们没有实现io.Reader接口:

无法将headers(类型为map[string]string)用作http.NewRequest的参数类型io.Reader:
map[string]string没有实现io.Reader(缺少Read方法)

这是我尝试的第一个代码片段,我实际上是从我认为可靠的来源获取的,显然它无法编译。Go看起来非常有趣,但几乎没有教程和有用的文档...请帮助我,应该如何正确做到这一点?

英文:

So, I've just started with Go and I'm trying to learn HTTP package. I found this program on the internet

headers := make(map[string]string)
headers["authorization"] = "1432536546" //the auhorization string

url :=    "anUrl\"

req, err := http.NewRequest("DELETE", url, headers)
if err != nil {
    panic(err)
}

But it is wrong, as it appears you can't use maps like this as they don't implement the io.Reader interface:

  cannot use headers (type map[string]string) as type io.Reader in argument to http.NewRequest:
  map[string]string does not implement io.Reader (missing Read method)

This is one of the first code snippets I tried, I actually got it from what I thought to be a reliable source, and obviously it doesn't compile. Go looks pretty interesting, but it has pretty much zero tutorials and useful documentation... Please help me, what is the right way to do it?

答案1

得分: 3

第三个参数是Body,用于POST请求。它不是用于头部的。
要添加头部,可以使用以下代码:

req, err := http.NewRequest("DELETE", url, nil)
if err != nil{
    panic(err);
}

req.Header.Add("Authorization","1432536546") //string, string

Go文档提供了几个示例,这个示例是Overview部分的第一个示例。

英文:

The third parameter is Body, which is used in POST requests. It is not for headers.
To add headers,

req, err := http.NewRequest("DELETE", url, nil)
if err != nil{
	panic(err);
}

req.Header.Add("Authorization","1432536546") //string, string

The Go documentation does provide several examples. This one is the first example in the Overview section.

答案2

得分: 2

你的假设是正确的,你不能以那种方式使用net/http包中的NewRequest方法。

此外,这样做也没有太多意义,因为NewRequest函数的第三个参数应该是请求的正文负载,而不是头部。

func NewRequest(method, urlStr string, body io.Reader) (*Request, error)

假设库没有错误,我猜测这里的http包不是net/http包,而是作为http导入的其他某种HTTP客户端。实际上,如果你导入

import (
  "github.com/foobar/someclient/http"
)

它也会被引用为http。你应该检查文件的导入。

顺便说一下,添加头部的正确方法是:

req, err := http.NewRequest("DELETE", "/path/to/something", nil)
if err != nil {
	// 处理错误
}
req.Header.Add("authorization", "1432536546")
英文:

Your assumption is correct, you can't use the NewRequest method from the net/http package in that way.

Moreover, it doesn't make a lot of sense as the third parameter of the NewRequest function is supposed to be the body payload of the request, not the headers.

func NewRequest(method, urlStr string, body io.Reader) (*Request, error)

Assuming the library is not incorrect, My assumption is that the http package here is not the net/http package, rather some sort of other HTTP client that is imported as http. In fact, if you import

import (
  "github.com/foobar/someclient/http"
)

it will also be referenced as http. You should check the import of the file.

FYI, the correct way to add headers is:

req, err := http.NewRequest("DELETE", "/path/to/something", nil)
if err != nil {
	// do something
}
req.Header.Add("authorization", "1432536546")

huangapple
  • 本文由 发表于 2016年2月15日 20:01:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/35408860.html
匿名

发表评论

匿名网友

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

确定