티스토리 뷰

그동안 프로젝트를 하면서 GET메소드밖에 쓸 일이 없어서 헤더나 바디에 뭘 넣어서 보내본 적이 없었다...🥲 오직..GET data만을 외치며 걸어온 인생...⭐️ 최근들어 axios 통신에 바디랑 헤더 넣어서 보내는 걸 알게돼서 기록해 둔다.

 

axios.get()에 headers 넣기

// axios.get(url, {headers: {키 : 밸류}})
axios
  .get('http://localhost:8000/posts', {
    headers: { Authorization: localStorage.getItem('token') },
  })

 

axios.post()에 body 넣기

// axios.get(url, {키 : 밸류})
axios
.post(
  'http://localhost:8000/posts',
  { content: textValue }
)

 

axios.post()에 headers와 body넣기

// axios.get(url, {바디로 보내려는 키 : 바디로 보내려는 밸류}, {headers: {키 : 밸류}})
axios
    .post(
      'http://localhost:8000/posts',
      { content: textValue },
      { headers: { Authorization: localStorage.getItem('token') } }
    )

 

참고 1 : Axios 공식문서

참고 2

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함