Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
biggerevil opened this issue Jan 16, 2025 · 3 comments · Fixed by #956
Closed

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

biggerevil opened this issue Jan 16, 2025 · 3 comments · Fixed by #956
Assignees
Labels
bug v2 For resty v2

Comments

@biggerevil
Copy link

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
@jeevatkm
Copy link
Member

@biggerevil Thanks for reaching out. I will analyze the SetAuthScheme behavior and get back to you.

@jeevatkm jeevatkm self-assigned this Jan 17, 2025
@jeevatkm jeevatkm added bug v2 For resty v2 and removed analysis labels Jan 17, 2025
@jeevatkm
Copy link
Member

@biggerevil I have added a fix, it is currently available in the branch fix-auth-scheme-override-empty. Can you try and share feedback?

@biggerevil
Copy link
Author

@jeevatkm works like a charm, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug v2 For resty v2
Development

Successfully merging a pull request may close this issue.

2 participants