- 회원가입 시 content type이 multipart/form-data와 application/json를 함께 보내야 했기 때문에, 합치는 로직이 필요했음
- 이미지 업로드를 위한 imageFormData와 데이터를 JSON 형식으로 변환한 후 Blob으로 감싼 jsonDataBlob을 따로 생성
- 두 개의 FormData 객체를 합쳐서 formData에 추가하고 서버로 전송
// 회원가입
export const registerMember = async ({ email, nickname, password, image, food }: registerType) => {
const imageFormData = new FormData();
if (image) {
imageFormData.append('file', image);
}
const jsonDataBlob = new Blob([JSON.stringify({ email, nickname, password, food })], {
type: 'application/json',
});
const jsonFormData = new FormData();
jsonFormData.append('request', jsonDataBlob);
try {
for (const [key, value] of jsonFormData.entries()) {
imageFormData.append(key, value);
}
const result = await axios.post('/api/member/signup', imageFormData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
return result.data;
} catch (error) {
console.error(error);
}
};