'create post on vue js
i want to create a post end send to api but i need to retrive userId to create my post.
( frontend : vuejs3 + axios backend : node + prisma + sql)
Test on postman is ok but i cannot retrive usedId in vue js. can you help me please ?
import axios from "axios"
export default {
name: 'PostCreate',
data () {
return {
title: '',
content: '',
}
},
methods: {
async createPost(req) {
const token = Authorization.split(' ')[1];
// dechiffre le token a l'aide de la clé secrete et du token presant dans authorization
const decodedToken = jwt.verify(token, `${process.env.MY_TOKEN}`);
console.log('decodedToken');
const response = await axios.post('http://localhost:3000/api/post', {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('token')
},
title: this.title,
content: this.content,
userId: decodedToken.userId
});
}
}
}
Solution 1:[1]
You should define headers as the 3rd argument.
const response = await axios.post('http://localhost:3000/api/post', {
title: this.title,
content: this.content,
userId: decodedToken.userId
}, {
headers: { Authorization: 'Bearer ' + localStorage.getItem('token') },
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomasz Staszkiewicz |
