# * Using Fetch

Written by ๐Ÿ“ Juunone

# REST API Connect

Reference

API Connenctor Fetch ์‚ฌ์šฉ๋ฒ•.

# Fetch

์œ„ ๋งํฌ๋ฅผ ์ฐธ์กฐ๋กœ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ด๋‹ค.

  • Fetch API๋ฅผ ์ด์šฉํ•˜๋ฉด Request๋‚˜ Response์™€ ๊ฐ™์€ HTTP์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ๋ฅผ ์กฐ์ž‘ํ•˜๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ fetch() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋น„๋™๊ธฐ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ์•Œ๊ธฐ์‰ฝ๊ฒŒ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • jQuery.ajax() ์™€ ์ฐจ์ด์ 

    • fetch()๋กœ ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜๋˜๋Š” Promise ๊ฐ์ฒด๋Š” HTTP error ์ƒํƒœ๋ฅผ rejectํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ok ์ƒํƒœ๊ฐ€ false์ธ resolve๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉฐ, ๋„คํŠธ์›Œํฌ ์žฅ์• ๋‚˜ ์š”์ฒญ์ด ์™„๋ฃŒ๋˜์ง€ ๋ชปํ•œ ์ƒํƒœ์—๋Š” reject๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
    • ๋ณดํ†ต fetch๋Š” ์ฟ ํ‚ค๋ฅผ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํŠธ์—์„œ ์‚ฌ์šฉ์ž ์„ธ์…˜์„ ์œ ์ง€ ๊ด€๋ฆฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ธ์ฆ๋˜์ง€ ์•Š๋Š” ์š”์ฒญ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž๊ฒฉ์ฆ๋ช…(credentials) ์˜ต์…˜์„ ๋ฐ˜๋“œ์‹œ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

# Basic

๊ธฐ๋ณธ์ ์ธ fetch ์ž‘์„ฑ๋ฒ•

fetch("https://jsonplaceholder.typicode.com/todos/1")
  .then(response => response.json())
  .then(json => console.log(json));

response : {
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}
1
2
3
4
5
6
7
8
9
10

# Headers

Headers ์ธํ„ฐํŽ˜์ด์Šค์—์„œ Headers() ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํ—ค๋” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

const content = "Hello World!";
const awesomeHeaders = new Headers();
awesomeHeaders.append("Content-Type", "application/json");
awesomeHeaders.append("Content-Length", content.length.toString());

//๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ•˜๋Š”๊ฒƒ์œผ๋กœ ์ƒ์„ฑํ• ์ˆ˜๋„ ์žˆ๋‹ค.
const awesomeHeaders = new Headers({
  "Content-Type": "application/json",
  "Content-Length": content.length.toString()
});
1
2
3
4
5
6
7
8
9
10

# Body

Request, Resposne ๋‘˜๋‹ค Body๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
body๋Š” ์•„๋ž˜์—์„œ ๊ธฐ์ˆ ํ•œ ํƒ€์ž…๋“ค ์ค‘ ํ•˜๋‚˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ArrayBuffer
  • ArrayBufferView (Uint8Array๊ฐ™์€ TypedArray)
  • Blob/File
  • ๋ฌธ์ž์—ด
  • URLSearchParams
  • FormData

Body๋ฅผ ๋‹ค๋ฃจ๊ธฐ์œ„ํ•ด์„œ ์ œ๊ณต๋˜๋Š” Method๊ฐ€ ์žˆ๋‹ค.

  • arrayBuffer()
  • blob()
  • json()
  • text()
  • formData()
fetch("https://jsonplaceholder.typicode.com/posts", {
  method: "POST",
  body: JSON.stringify({
    title: "foo",
    body: "bar",
    userId: 1
  }),
  headers: {
    "Content-type": "application/json; charset=UTF-8"
  }
})
  .then(response => response.json())
  .then(json => console.log(json));
1
2
3
4
5
6
7
8
9
10
11
12
13

# Request

๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ ์šฉํ•˜๋Š”๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ˆ˜์˜ ์„ค์ •์„ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์žˆ๋Š” ์ดˆ๊ธฐํ™” ์˜ต์…˜์ด๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์ด ๋‘๋ฒˆ์งธ ์ธ์ž์— ๋“ค์–ด๊ฐˆ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค์ด๋‹ค.

  • method
  • headers
  • body
  • mode
  • cache
  • credentials
  • redirect
  • referrer
  • integrity
const init = {
  method: "GET",
  mode: "cors", //The mode you want to use for the request, e.g., cors, no-cors, or same-origin.
  cache: "default" // The cache mode you want to use for the request.
};

fetch("train.jpg", init)
  .then(function(response) {
    return response.blob();
  })
  .then(function(myBlob) {
    const objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Query parameters

์ฟผ๋ฆฌ ๋งค๊ฐœ ๋ณ€์ˆ˜๋„ ์ง€์›๋œ๋‹ค.

fetch("https://jsonplaceholder.typicode.com/posts?userId=1")
  .then(response => response.json())
  .then(json => console.log(json));
1
2
3
Last Updated: 7/5/2019, 8:38:55 AM