Ask: how does vue take cookie to access django (using the methods in rest API)?

problem description

vue uses axios to access the backend:

    this.$axios.get(
    "/api/v2/server/singleServer/",
    {params:
    {userid:that.$store.state.userData.id,token:that.$cookies.get("token"),serverid:that.$route.params.serverid}
    }
   ,
    {headers:{"content-type":"application/json"}}, 
  ).then(function(ret){
    console.log(ret.data)
   that.loading = false
 that.serverData = ret.data
  })
  

I wrote cookie in url, and I can get it. Now, what if you put the cookie in the header and get it through the back end?


see that your code is written in the parameters, this is not very good, the correct posture should be unified processing.
has a request header Authentication, that can be placed in the request header, or customize a request header and have the backend release the request header.
restAPI, you need to bring the authentication token every time. You can set the header, backend to retrieve the token in the request header in the request interceptor of axios.


you can write a middleware according to your own needs. It is recommended to take a look at the source code of SessionMiddleware. You can see it at a glance.

Menu