-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauthorizedPost.js
40 lines (35 loc) · 1.11 KB
/
authorizedPost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const axios = require("axios");
const { PORT } = require("../config/constants");
async function meExample() {
try {
// 1. first login to get a token
// this assumes you seeded the test user using sequelize cli seeds
// if you havent follow the setup steps from the readme first
const responseLogin = await axios.post(`http://localhost:${PORT}/login`, {
email: "[email protected]",
password: "test1234"
});
console.log("RESPONSE FROM SERVER", responseLogin.data);
const token = responseLogin.data.token;
// 2. now we can use the token in the /me endpoint to get
// the email & name of this user
const response = await axios.post(
`http://localhost:${PORT}/authorized_post_request`,
{
// this is our request body
title: "My new thing",
moreData: "I like turtles"
},
{
headers: {
Authorization: `Bearer ${token}`
}
}
);
console.log(response.data);
} catch (error) {
console.log("OH NO AN ERROR", error.message);
console.log("WHAT HAPPENED?", error.response.data);
}
}
meExample();