简体   繁体   English

如何将 Base64 发送到 API

[英]How to send Base64 to API

I am ultimately trying to send a fax with the Vitelity API .我最终尝试使用Vitity API发送传真。 I have an API on EC2 that I am calling from my React Native app:我从我的 React Native 应用程序调用的 EC2 上有一个 API:

// 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));
  };

However, this returns an error cannot POST .但是,这会返回错误cannot POST If, instead of b64 , I change data1 's value to something like jdl3439fdjsle/jjug , everything is great.如果我将data1的值改为jdl3439fdjsle/jjug而不是b64 ,那么一切都很好。

Do I need to do something special to b64 before I can send it?在发送之前我需要对b64做一些特别的事情吗?

My Base64 looks like: /9j/4AA{1.2m more chars}uB//9k= .我的 Base64 看起来像: /9j/4AA{1.2m more chars}uB//9k= I've pasted it into a converter and it produces the correct image.我已将其粘贴到转换器中,它会生成正确的图像。

I geuss you have to use a Multipart Form with multipart/form-data as content-type headers if you want to send images.如果你想发送图像,我猜你必须使用带有multipart/form-data作为content-type标题的多部分表单。 See also this question .另请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM