I have 2 requests, the first one logs in to the server and the second one makes an authorized request.
This is a first request:
func (f *Service) login() error {
f.cookieJar, _ = cookiejar.New(nil)
client := &http.Client{
Jar: f.cookieJar,
}
cr, _ := json.Marshal(f.Credentials)
request, err := http.NewRequest("POST", f.BaseUrl+"/Login/", bytes.NewBuffer(cr))
// handle error
request.Header.Set("Content-Type", "application/json")
resp, err := client.Do(request)
// handle error
defer resp.Body.Close()
//ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return errors.New("unable to login, check you login and password")
}
return nil
}
And this is the second request:
err = f.login()
if err != nil {
return err
}
request, err := http.NewRequest("POST", f.BaseUrl+"/invoices/writeoff", bytes.NewBuffer(r))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
client := &http.Client{
Jar: f.cookieJar,
}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
As you guessed the second request depends on the first because it used cookies from the first request.
The problem is in the commented line ioutil.ReadAll(resp.Body)
in the first request. If they are commented/removed the second request does not work. It looks like the cookies are wrong.
If I uncomment that lines the second request works.
I don't understand what is going wrong if I comment that lines.
Comments
Post a Comment