RefreshSessionTokenRaw POST credentials to the /session endpoint and get a session cookie
(credentialsJSON []byte)
| 392 | // RefreshSessionTokenRaw POST credentials to the /session endpoint and get a |
| 393 | // session cookie |
| 394 | func (client *Client) RefreshSessionTokenRaw(credentialsJSON []byte) error { |
| 395 | postURL := fmt.Sprintf("%s/rest/auth/1/session", client.serverURL) |
| 396 | |
| 397 | req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(credentialsJSON)) |
| 398 | if err != nil { |
| 399 | return err |
| 400 | } |
| 401 | |
| 402 | urlobj, err := url.Parse(client.serverURL) |
| 403 | if err != nil { |
| 404 | fmt.Printf("Failed to parse %s\n", client.serverURL) |
| 405 | } else { |
| 406 | // Clear out cookies |
| 407 | client.Jar.SetCookies(urlobj, []*http.Cookie{}) |
| 408 | } |
| 409 | |
| 410 | if client.ctx != nil { |
| 411 | ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout) |
| 412 | defer cancel() |
| 413 | req = req.WithContext(ctx) |
| 414 | } |
| 415 | |
| 416 | response, err := client.Do(req) |
| 417 | if err != nil { |
| 418 | return err |
| 419 | } |
| 420 | |
| 421 | defer response.Body.Close() |
| 422 | |
| 423 | if response.StatusCode != http.StatusOK { |
| 424 | content, _ := io.ReadAll(response.Body) |
| 425 | return fmt.Errorf( |
| 426 | "error creating token %v: %s", response.StatusCode, content) |
| 427 | } |
| 428 | |
| 429 | data, _ := io.ReadAll(response.Body) |
| 430 | var aux SessionResponse |
| 431 | err = json.Unmarshal(data, &aux) |
| 432 | if err != nil { |
| 433 | return err |
| 434 | } |
| 435 | |
| 436 | var cookies []*http.Cookie |
| 437 | cookie := &http.Cookie{ |
| 438 | Name: aux.Session.Name, |
| 439 | Value: aux.Session.Value, |
| 440 | } |
| 441 | cookies = append(cookies, cookie) |
| 442 | client.Jar.SetCookies(urlobj, cookies) |
| 443 | |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | // ============================================================================= |
| 448 | // Endpoint Wrappers |