go-client icon indicating copy to clipboard operation
go-client copied to clipboard

GO client does not return headers from http response

Open arihant30 opened this issue 3 years ago • 1 comments

The Login API is supposed to sign-in a User and return a "Set-Cookie" header that contains the refresh token and access token, but the client does not read the headers from the response, nor does it return them. A workaround is to call the login API yourself instead of using the go-client and reading the headers from the response.

func (rc *restClient) Do() error {
	req, err := http.NewRequest("POST", rc.Uri.String(), rc.Body)
	if err != nil {
		return err
	}
	for key, val := range rc.Headers {
		req.Header.Set(key, val)
	}
	resp, err := rc.HTTPClient.Do(req)
	if err != nil {
		return err
	}
// Reading the headers from the response
	key := resp.Header.Values("Set-Cookie")
	if key != nil {
		rc.Cookie = key
	}
	defer resp.Body.Close()
	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		return errors.BaseBadRequest
	} else {
		rc.ErrorRef = nil
		if _, ok := rc.ResponseRef.(*BaseHTTPResponse); !ok {
			err = json.NewDecoder(resp.Body).Decode(rc.ResponseRef)
		}
	}
	return err
}

This is a workaround that I have used.

arihant30 avatar Feb 04 '22 20:02 arihant30

Thanks for the feedback @arihant30 !

The client libraries cover ~95% of the functionality of the REST API, but not 100%. Cookies, JWT authentication, and http status codes are some of the holes.

I'm leaving this open for possible future work (which would take place in https://github.com/fusionauth/fusionauth-client-builder so that every client library could benefit), but since there's a straightforward workaround, can't commit to a timeline.

mooreds avatar Feb 06 '22 16:02 mooreds