[1] JSON 객체 전송
1) 서버에서 파일로 받아들인다면
Blob 객체를 만드는 것이 가장 좋다
const obj = {
hello: "world"
};
const json = JSON.stringify(obj);
const blob = new Blob([json], {
type: 'application/json'
});
const data = new FormData();
data.append("document", blob);
axios({
method: 'post',
url: '/sample',
data: data,
})
2) 서버에서 JSON으로 받아들인다면
- 그냥
JSON.stringify()
로 감싼 후 보내면 되는데, type
만 추가로 설정하면 된다.
const request = { "title": "강의1", "classKind": "스쿠버 다이빙", "groupName": "AIDA", "certificateKind": "Level1", "description": "내용1", "price": 100000, "period": 4, "studentCount": 5, "region": "서울", "swimmingPoolId": 1, "equipmentList": [{ "name": "물안경", "price": 3000 }, { "name": "수영모", "price": 3000 }] }
const data = JSON.stringify(request);
const formdata = new FormData();
formdata.append('request', {
string: data,
type: 'application/json'
});
axios.post(URL.NewLecture, formdata, {
headers: {
'IsRefreshToken' : 'false',
'Content-Type': 'multipart/form-data; boundary=someArbitraryUniqueString',
}
}) .then((res) => console.log(res))
.catch((err) => console.log(err))
[2] 파일 전송
const formdata = new FormData();
formdata.append('fileList', {
name: 'hello.jpg', //파일명
type: image.type, //image/jpg, imgae/png 등이 된다.
uri: image.uri //파일의 절대경로
});
axios.post(URL.NewLecture, formdata, {
headers: {
'IsRefreshToken' : 'false',
'Content-Type': 'multipart/form-data; boundary=someArbitraryUniqueString',
}
}) .then((res) => console.log(res))
.catch((err) => console.log(err))