API - dummy API endpoint to test HTTP requests and responses

0 votes
33 views
added Feb 14 in API by lcjr First Warrant Officer (11,830 points)

When using a dummy API endpoint like JSONPlaceholder for testing file uploads, you typically won't be able to retrieve the uploaded file itself because JSONPlaceholder does not actually store or process file uploads. It's mainly intended for testing HTTP requests and responses.

However, you can still simulate the behavior of handling file uploads on the client-side and receiving a response from the server. Here's how you can modify your code to handle the response from the dummy API:

// Dummy endpoint for testing
const CSV_API_ENDPOINT = 'https://jsonplaceholder.typicode.com/posts';

const handleUpload = async () => {
const CSV_API_ENDPOINT = 'https://jsonplaceholder.typicode.com/posts'; 

const handleUpload = async () => {
  if (selectedFile) {
    try {
      const formData = new FormData();
      formData.append('csvFile', selectedFile);

      const response = await fetch(CSV_API_ENDPOINT, {
        method: 'POST',
        body: formData
      });

      if (!response.ok) {
        throw new Error('Failed to upload file');
      }

      const data = await response.json();
      console.log('Response from dummy API:', data);

      // Handle the response as needed for testing

    } catch (error) {
      console.error('Error uploading file:', error);
      // Handle error, show error message, etc.
    }
  }
};

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...