📝 Josh's Notes

Example POST Request using JavaScript

 1let newPost = {
 2  userId: 1,
 3  title: "My post",
 4  body: "This is my first post"
 5};
 6
 7fetch("https://jsonplaceholder.typicode.com/posts", {
 8  method: "POST",
 9  headers: {
10    "Content-Type": "application/json"
11  },
12  body: JSON.stringify(newPost)
13})
14  .then((rawResponse) => {
15    if (!rawResponse.ok) {
16      throw new Error(
17        `code: ${rawResponse.status}, status text: ${rawResponse.statusText}`
18      );
19    }
20    return rawResponse.json();
21  })
22  .then((jsonifiedResponse) =>
23    console.log("Jsonified data: ", jsonifiedResponse)
24  )
25  .catch((error) => console.log(error));

#javascript #programming #http