종우의 컴퓨터 공간

Custom HTTP Library (Fetch with Async Await) 본문

자바 스크립트 (JavaScript)/Asynchronous JS, Ajax & Fetch API

Custom HTTP Library (Fetch with Async Await)

종우공간 2021. 8. 17. 01:57
class EasyHttp {
  // Make a HTTP GET Request
  async get(url) {
    const response = await fetch(url);
    const data = await response.json();
    return data;
  }

  // Make a HTTP POST Request
  async post(url, data) {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-type': 'application/json'
      },
      body: JSON.stringify(data)
    });

    const resData = await response.json();
    return resData;
  }

  // Make a HTTP PUT Request
  async put(url, data) {
    const response = await fetch(url, {
      method: 'PUT',
      headers: {
        'Content-type': 'application/json'
      },
      body: JSON.stringify(data)
    });
    const resData = await response.json();
    return resData;
  }

  // Make a HTTP DELETE Request
  async delete(url) {
    const response = await fetch(url, {
      method: 'DELETE',
      headers: {
        'Content-type': 'application/json'
      }
    })
    const resData = await 'Resource Deleted...'
    return resData;
  }
}

'자바 스크립트 (JavaScript) > Asynchronous JS, Ajax & Fetch API' 카테고리의 다른 글

★Async & Await  (0) 2021.08.17
[ES6]Custom HTTP Library (Fetch with Promises)  (0) 2021.08.16
★[ES6]Arrow Functions  (0) 2021.08.13
★[ES6]Fetch API  (0) 2021.08.13
★[ES6] Promises  (0) 2021.08.11