'Expo Download file. FileSystem Download Async method POST with body

I need a way to make a request with method Post passing a body but I didnt find a way to do it. The documentation: https://docs.expo.io/versions/latest/sdk/filesystem/ only show the GET method, I need a way to make a post request passing the body.

FileSystem.downloadAsync(${baseUrl}/v1/paycheck/pdf, FileSystem.documentDirectory + ‘file.pdf’,
{
headers: {
‘Authorization’: localToken
},
httpMethod: ‘POST’,

            body: {
                type: 'monthy',
                year: '2021',
                month: 2,
                employer: {
                    name: "Pink",
                }
            }
        }
    )
        .then(({uri}) => {
            Sharing.shareAsync(uri, {dialogTitle: 'Salvar ou Compartilhar'})

        })
        .catch(error => {
            console.error(error);
        });
}


Solution 1:[1]

I finally managed to make it work using axios e FileReader();

const response = await axios.post(`${baseUrl}/v1/paycheck/pdf`, data, {responseType: 'blob'});
const fr = new FileReader();
fr.onload = async () => {
    const fileUri = `${FileSystem.documentDirectory}/document.pdf`;
    const result = await FileSystem.writeAsStringAsync(fileUri, fr.result.split(',')[1], {encoding: FileSystem.EncodingType.Base64});
    saveFile(fileUri);

};
fr.readAsDataURL(response.data);

Solution 2:[2]

As far as I understand your problem

My Approach for Downloading and Sharing the PDF would be

Writing these two functions

// Execute this function when you to share the file...
const GetPDF = async () => {
  try {
    const response = await fetch(`${baseUrl}/v1/paycheck/pdf`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: "localToken",
      },
      body: JSON.stringify({
        type: "monthy",
        year: "2021",
        month: 2,
        employer: {
          name: "Pink",
        },
      }),
    });

    const content = await response.json();
    DownloadThenShare(content); // Some URI
  } catch (error) {
    console.error(error);
  }
};

Now DownloadAndShare function

// This function will execute after Download has been completed successfully
const DownloadThenShare = async (uri) => {
  const downloadInstance = FileSystem.createDownloadResumable(
    uri,
    FileSystem.documentDirectory + "file.pdf"
  );

  const result = await FileSystem.downloadInstance.downloadAsync();

  if (result.status === 200) {
    Sharing.shareAsync(result.uri, { dialogTitle: "Salvar ou Compartilhar" });
  } else {
    console.log("Failed to Download");
  }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zoe stands with Ukraine
Solution 2 Kartikey