如何将邮递员生成的这段代码转换为使用 axios 的请求?
How do I convert this code generated by postman to a request using axios?
下面是邮递员生成的代码
var formdata = new FormData();
formdata.append("image", fileInput.files[0], "/path/to/file");
formdata.append("imageType", "Image_URL_1");
formdata.append("userID", "3");
formdata.append("password", "dsddfsdfsdf");
formdata.append("userImage", "");
var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};
fetch("http://localhost:3000/uploadUserImage", requestOptions)
.then(response => response.text())
.then(result => console.log(result))`enter code here`
.catch(error => console.log('error', error));
'
我曾尝试将其转换为 axios
幸运的是它会遇到 API 而不是 return 网络错误,但是,它会 return req
是 undefined
.
这是我当前的 axios 调用
async createFile(result){
let formData = new FormData();
formData.append("image", result, result.uri);
formData.append("imageType", "Image_URL_1");
formData.append("userID", this.state.userID);
formData.append("password", this.state.password);
try{
const res = await axios.post('http://162.249.2.147:3000/uploadUserImage', {
formData,
headers: {
'content-type': 'form-data',
},
})
.then(function(){ console.log('SUCCESS!!'); })
} catch(e){ console.log(e)}
}
您需要发送带有键 data
的表单数据。
let formData = new FormData();
formData.append("image", result, result.uri);
formData.append("imageType", "Image_URL_1");
formData.append("userID", this.state.userID);
formData.append("password", this.state.password);
try {
const res = await axios
.post("http://162.249.2.147:3000/uploadUserImage", {
data: formData,
headers: {
"content-type": "multipart/form-data",
},
})
.then(function () {
console.log("SUCCESS!!");
});
} catch (e) {
console.log(e);
}
}
下面是邮递员生成的代码
var formdata = new FormData();
formdata.append("image", fileInput.files[0], "/path/to/file");
formdata.append("imageType", "Image_URL_1");
formdata.append("userID", "3");
formdata.append("password", "dsddfsdfsdf");
formdata.append("userImage", "");
var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};
fetch("http://localhost:3000/uploadUserImage", requestOptions)
.then(response => response.text())
.then(result => console.log(result))`enter code here`
.catch(error => console.log('error', error));
'
我曾尝试将其转换为 axios
幸运的是它会遇到 API 而不是 return 网络错误,但是,它会 return req
是 undefined
.
这是我当前的 axios 调用
async createFile(result){
let formData = new FormData();
formData.append("image", result, result.uri);
formData.append("imageType", "Image_URL_1");
formData.append("userID", this.state.userID);
formData.append("password", this.state.password);
try{
const res = await axios.post('http://162.249.2.147:3000/uploadUserImage', {
formData,
headers: {
'content-type': 'form-data',
},
})
.then(function(){ console.log('SUCCESS!!'); })
} catch(e){ console.log(e)}
}
您需要发送带有键 data
的表单数据。
let formData = new FormData();
formData.append("image", result, result.uri);
formData.append("imageType", "Image_URL_1");
formData.append("userID", this.state.userID);
formData.append("password", this.state.password);
try {
const res = await axios
.post("http://162.249.2.147:3000/uploadUserImage", {
data: formData,
headers: {
"content-type": "multipart/form-data",
},
})
.then(function () {
console.log("SUCCESS!!");
});
} catch (e) {
console.log(e);
}
}