Skip to content

SetAuthScheme("") doesn't remove default prefix "Bearer " #954

Closed
@biggerevil

Description

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"
)

func main() {
	// Start a local HTTP server in the background
	go func() {
		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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugv2For resty v2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions