You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my case, I needed to send a token without any prefixes. I attempted to achieve this by using SetAuthScheme(""), but it still added the "Bearer " prefix. The only workaround I found was to set the Authorization header explicitly instead of using SetAuthToken().
I'm not sure if this is considered a bug, but I found the behavior unexpected. It took me some time to diagnose why my code wasn't working as expected.
Here is an example:
package main
import (
"fmt""log""net/http""github.com/go-resty/resty/v2"
)
funcmain() {
// Start a local HTTP server in the backgroundgofunc() {
http.HandleFunc("/", func(w http.ResponseWriter, r*http.Request) {
fmt.Println("Authorization header:", r.Header.Get("Authorization"))
w.WriteHeader(http.StatusOK)
_, _=w.Write([]byte("OK"))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}()
fmt.Println("1) Resty without setting auth scheme:")
resty.New().
SetAuthToken("MY_SUPER_SECRET_TOKEN").
R().Get("http://localhost:8080/")
fmt.Println()
fmt.Println("2) Resty with setting auth scheme to \"\":")
resty.New().
SetAuthScheme("").
SetAuthToken("MY_SUPER_SECRET_TOKEN").
R().Get("http://localhost:8080/")
fmt.Println()
fmt.Println("3) Resty with setting header Authorization:")
resty.New().
SetHeader("Authorization", "MY_SUPER_SECRET_TOKEN").
R().Get("http://localhost:8080/")
fmt.Println()
}
Output:
1) Resty without setting auth scheme:
Authorization header: Bearer MY_SUPER_SECRET_TOKEN
2) Resty with setting auth scheme to "":
Authorization header: Bearer MY_SUPER_SECRET_TOKEN
3) Resty with setting header Authorization:
Authorization header: MY_SUPER_SECRET_TOKEN
The text was updated successfully, but these errors were encountered:
In my case, I needed to send a token without any prefixes. I attempted to achieve this by using
SetAuthScheme("")
, but it still added the "Bearer " prefix. The only workaround I found was to set the Authorization header explicitly instead of usingSetAuthToken()
.I'm not sure if this is considered a bug, but I found the behavior unexpected. It took me some time to diagnose why my code wasn't working as expected.
Here is an example:
Output:
The text was updated successfully, but these errors were encountered: