Home > Blockchain >  How to send Base64 to API
How to send Base64 to API

Time:12-03

I am ultimately trying to send a fax with the Vitelity API. I have an API on EC2 that I am calling from my React Native app:

// 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. If, instead of b64, I change data1's value to something like jdl3439fdjsle/jjug, everything is great.

Do I need to do something special to b64 before I can send it?

My Base64 looks like: /9j/4AA{1.2m more chars}uB//9k=. I've pasted it into a converter and it produces the correct image.

CodePudding user response:

I geuss you have to use a Multipart Form with multipart/form-data as content-type headers if you want to send images. See also this question.

  • Related