I have an API written in Golang and a client in C#, they work with Bearer
token, on the client I add the Authorization
header but on the server side it is blank.
This is the client C#:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
//request.PreAuthenticate = true;
request.ContentType = "application/json";
request.Method = "PUT";
request.Headers.Add("Authorization", $"Bearer {token}");
//request.Accept = "application/json";
using (StreamWriter stream = new StreamWriter(request.GetRequestStream()))
{
string serialized = JsonConvert.SerializeObject(sync, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd" });
stream.Write(serialized);
}
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (((int)response.StatusCode) < 200 || ((int)response.StatusCode) >= 300)
{
throw new Exception("error");
}
}
}
catch (WebException wex)
{
var body = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
throw new HttpRequestException(body);
}
Debugging it, the token is present.
This is the golang server code:
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
authHeader := r.Header.Get("Authorization")
_, err := parseAuthHeader(ctx, verifier, authHeader)
if err != nil {
render.Render(w, r, httperr.ErrUnauthorized(err.Error()))
return
}
next.ServeHTTP(w, r)
})
}
The variable authHeader
is blank, but only in production (Google Cloud). If I run it locally, it works. Already tried to add PreAuthenticate
, did not help. Any idea?
Well, I used the http
instead the https
api link. Adding the s
solved the problem.