api/session Unauthorized

JeffersonG4 years ago

hi.
i am trying to make session connection, when i make the post i receive the user object and the cookie is set, but when i try to use cookie to get the session o delete it i receive the unauthorized 401 response.

this is how i am making the session post request.

          const traccarUrlSession = "http://localhost:8082/" + "api/session"
         
          var myHeaders = new Headers();
          myHeaders.append('Content-Type', 'application/x-www-form-urlencoded');
          myHeaders.append('Accept', '*/*');

          var urlencoded = new URLSearchParams();
          urlencoded.append("email", email);
          urlencoded.append("password", password);

          var requestOptions = {
               method: 'POST',
               headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
               },
               body: urlencoded,
          };
          
          let sessionResponse = await fetch(traccarUrlSession, requestOptions)
          
          let sessionResult = await sessionResponse.json()
          console.log(sessionResult)

i got the user object and the cookie is set.

and this is how i am making the session delete request.

          const traccarUrlSession = "http://localhost:8082/" + "api/session"
          const cookies = Cookies.get()
          
          const cookieHeader = `JSESSIONID=${cookies.JSESSIONID}`
          //const cookieHeader = "JSESSIONID=node01sxgn4hgq8gf1x5q39tzxbdxg102.node0"
          
          var myHeaders = new Headers();
          myHeaders.append("Accept", "application/json");
          myHeaders.append("Cookie", cookieHeader);
          
          var urlencoded = new URLSearchParams();
          
          var requestOptions = {
              method: 'DELETE',
              headers: myHeaders,
              body: urlencoded,
              redirect: 'follow'
          };

          let logOutResponse = await fetch(traccarUrlSession, requestOptions)
          
          let logOutResult= await logOutResponse.json()
          console.log(logOutResult)

this is the response i got in the browser console:
DELETE http://localhost:8082/api/session 401 (Unauthorized)

could you please help me and let me know what am i doing wrong, or how can i solve this.

thanks

Anton Tananaev4 years ago

It looks like you are trying to set cookie header manually. I don't think it works like that.