I have a use case that necessitates the inclusion of form data, including a file, within the body of an HTTP request. How can I accomplish this using Apex?

I aim to replicate a similar request to the one illustrated below, which is implemented in JavaScript.

var headers = new Headers();
    headers.append("Authorization", `Bearer ${document.getElementById('bearer').value}`);

    var formdata = new FormData();
    var docInfo = { name: 'test-file.txt', description: 'Test Description', CategoryId: 1};
    var schoolId = document.getElementById('schoolId').value;
    var host = document.getElementById('host').value;

    formdata.append("binary", fileInput.files[0], "Test.txt");
    formdata.append("data", JSON.stringify(docInfo));

    var requestOptions = {
        method: 'POST',
        headers: headers,
        body: formdata,

    fetch(`https://${host}/api/admissions/applicants/${schoolId}/documents`, requestOptions)
    .then(response => response.text())
    .then(result => {
        document.getElementById('output').value = result;
    .catch(error => {
        console.log('error', error);
        document.getElementById('output').value = result;


Browse other questions tagged .