Oddbean new post about | logout
 #Biden’s failure to confront #China, now waging proxy wars on three continents, means #XiJinping undoubtedly sees a green light for starting even more conflicts. 
  * @param {object} params - object containing the parameters for the api request
* @returns {object} response from the api
*/
import axios from 'axios';
const baseUrl = process.env.REACT_APP_API_URL || "http://localhost:8080";

//create post
export const createPost = (params) => {
  return new Promise((resolve, reject) => {
    axios.post(baseUrl + '/posts', params)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
};
//get all posts
export const getAllPosts = (params) => {
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + '/posts', params)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
};
//get one post
export const getOnePost = (params) => {
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + '/posts/' + params)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
};
//update post
export const updatePost = (params) => {
  return new Promise((resolve, reject) => {
    axios.put(baseUrl + '/posts/' + params)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
};
//delete post
export const deletePost = (params) => {
  return new Promise((resolve, reject) => {
    axios.delete(baseUrl + '/posts/' + params)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
}; 
 



<a href="https://www.googleapis.com/auth/drive" class="button button-primary">Authorize Drive API</a>
<button type="submit" class="button button-success">Upload File</button>
<hr>
<div class="row">
    <div class="col-md-6">
        <label for="title">Title:</label>
        <input type="text" id="title" name="title" class="form-control">
    </div>
    <div class="col-md-6">
        <label for="description">Description:</label>
        <textarea id="description" name="description" rows="5" cols="30"></textarea>
    </div>
</div>
<br>
<h4>Select file to upload:</h4>
<input type="file" id="file_to_upload" name="file_to_upload">
<hr>

<script>
function handleFileUpload() {
  const input = document.getElementById('file_to_upload');
  const reader = new FileReader();
  reader.onloadend = function (e) {
    if (this.status === 0 && e.target.files[0]) {
      upload(reader, e.target.files[0]);
    }
  }
  input.addEventListener('change', handleFileUpload);
}

function upload(reader, file) {
  const xhr = new XMLHttpRequest();
  xhr.open('POST', '/upload');
  xhr.setRequestHeader('Content-Type', file.type);
  xhr.onload = function () {
    if (xhr.status === 200) {
      alert(xhr.responseText);
    } else {
      console.log(xhr.status, xhr.statusText);
    }
  };
  reader.readAsBinaryString(file);
}
</script>