search

Home  >  Q&A  >  body text

Axios and Fetch in the browser do not carry cookies when sending requests

I developed a spring boot api using jwt auth and I got the endpoint 'http://localhost:8080/api/signin' If the login is successful, it will return a JSESSIONID, which is a jwtToken. So far so good, but I can get other methods from the API in the browser since the cookie is not passed in the request body.

The cookie generated by jwt is as follows: JSESSIONID=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTY4NTE1Mzk5NSwiZXhwIjoxNjg1MjQwMzk1fQ.9jxHyzDJKVra8IryxbH8se0xSl4_Dka p NsKmjRCvlJs_R8M3x3RBMeo-1VPAJv6YSQwC6ukJutRwGEyfeYrGwQ;path=/;Http only;expire=Sunday, May 28, 2023 02:38:33 GMT;

For example: If I do this

1

curl --location 'http://localhost:8080/user/admin/all' --header 'Cookie: JSESSIONID=mycookie'

Everything goes fine and returns the json I want, but when I run it on the browser, in the js script on my frontend

1

2

3

4

5

6

7

8

9

10

11

12

13

14

axios

  .get("http://localhost:8080/user/admin/all", {

    headers: {

      Cookie:

        "JSESSIONID=mycookie",

    },

  })

  .then((response) => {

    console.log(response.headers);

    console.log(response.data);

  })

  .catch((error) => {

    console.error("Erro:", error);

  });

I get this error:

1

GET http://localhost:8080/user/admin/all 401

Cookie not set. I tried so many things and almost lost my mind. Do you know what it will be?

P粉662361740P粉662361740280 days ago531

reply all(1)I'll reply

  • P粉988025835

    P粉9880258352024-04-05 12:36:43

    You need to set withCredentials: true

    reply
    0
  • Cancelreply