The Header struct in internet/http lib has a format of map[string][]string, is there any attainable means to make use of such lib to set a header of map[string]string? The host fails to parse the Authorization header worth, as a result of it’s map not a string returning 401
http.Header
is simply the in-memory kind outlined in Go. When the HTTP request is definitely despatched, the headers are all simply a part of the request message and so they look the identical as some other HTTP request header:
Content material-Sort: utility/json
Settle for: utility/json
Authorization: primary abc123...
So the truth that they had been a []string
vs. string
isn’t identified by the server.
The HTTP commonplace permits a number of values to be related to a single area by becoming a member of them with commas or repeating the sector title. Should you put values right into a slice, a kind of two issues will occur when the request is generated.
Contemplating you talked about a problem parsing the Authorization header worth, I’m guessing (if I’m flawed, are you able to please embody the code you’re utilizing, the error message on the server, something in order that we may help you troubleshoot) the difficulty is that you simply’re setting the Authorization scheme and worth as separate values within the slice (e.g. req.Headers["Authorization"] = []string{"primary", "abc123..."}
which is inflicting the Authorization header area to look as Authorization: primary, abc123...
or
Authorization: primary
Authorization: abc123...
Which might be not accurately parseable by the server. You need to as a substitute Set the Authorization worth as a single string:
req.Header.Set("Authorization", "primary abc123...")
If this doesn’t reply your query, please present extra data like your code, the error from the server, and so on. to assist us provide help to
1 Like
Thanks Sean for getting again to me. The token is legitimate and I examined it with requests
lib from py. Appears to be like prefer it additionally fails with curl, so there must be no downside utilizing internet/http
Request
header := http.Header{}
header.Add("Content material-Sort", "utility/json")
header.Set("Authorization", "Bearer "+t)
resp, err := http.Shopper.Do(&http.Request{
Methodology: "GET",
URL: url,
Header: header,
})
Code Response
"{n "error": {n "code": 401,n
"message": "Request had invalid authentication credentials. Anticipated OAuth 2 entry token,
login cookie or different legitimate authentication credential. See https://builders.google.com/identification/sign-in/net/devconsole-project.",n
"standing": "UNAUTHENTICATED"n }n}n"
internet/http
works effective, I did a curl -v
and it appears that evidently I get this beneath error.
Curl Response
< www-authenticate: Bearer realm="https://accounts.google.com/", error="insufficient_scope", scope="SOME_SCOPE">
{
"error": {
"code": 403,
"message": "Request had inadequate authentication scopes.",
"standing": "PERMISSION_DENIED"
}
Someway I bypassed the 401 in curl however not in my code. As soon as I repair the curl response, I must be good.