如何将 Base64 发送到 API
How to send Base64 to API
我最终想用 Vitelity API 发送传真。我在 EC2 上有一个 API,我正在从我的 React Native 应用程序调用它:
// Encoding to Base64
const encodeB64 = () => {
RNFS.readFile(croppedImage, 'base64').then(res => {
sendB64(res);
});
};
const sendB64 = b64 => {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
let raw = JSON.stringify({
data1: b64, // 'jdl3439fdjsle/jjug'
login: {login},
pass: {pass},
faxnum: {destinationNum},
faxsrc: {sourceNum},
recname: 'Test',
file1: 'testfax.jpg',
});
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
};
fetch(API_URL, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
};
然而,这returns一个错误cannot POST
。如果我将 data1
的值改为 jdl3439fdjsle/jjug
,而不是 b64
,那么一切都很好。
在发送之前我需要对 b64
做一些特殊的事情吗?
我的 Base64 看起来像:/9j/4AA{1.2m more chars}uB//9k=
。我已将它粘贴到转换器中,它生成了正确的图像。
我想如果您想发送图像,您必须使用 multipart/form-data
和 content-type
headers 的多部分表单。另见 question.
我最终想用 Vitelity API 发送传真。我在 EC2 上有一个 API,我正在从我的 React Native 应用程序调用它:
// Encoding to Base64
const encodeB64 = () => {
RNFS.readFile(croppedImage, 'base64').then(res => {
sendB64(res);
});
};
const sendB64 = b64 => {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
let raw = JSON.stringify({
data1: b64, // 'jdl3439fdjsle/jjug'
login: {login},
pass: {pass},
faxnum: {destinationNum},
faxsrc: {sourceNum},
recname: 'Test',
file1: 'testfax.jpg',
});
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
};
fetch(API_URL, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
};
然而,这returns一个错误cannot POST
。如果我将 data1
的值改为 jdl3439fdjsle/jjug
,而不是 b64
,那么一切都很好。
在发送之前我需要对 b64
做一些特殊的事情吗?
我的 Base64 看起来像:/9j/4AA{1.2m more chars}uB//9k=
。我已将它粘贴到转换器中,它生成了正确的图像。
我想如果您想发送图像,您必须使用 multipart/form-data
和 content-type
headers 的多部分表单。另见 question.