File Upload Timeout - POST /api/documents (502)

282 views
Skip to first unread message

Jeffrey S

unread,
Apr 9, 2021, 9:29:05 AM4/9/21
to DrChrono API Developers
Just trying to get basic file upload working, I've gotten 400 responses and corrected my request, but now it is timing out (60 seconds) with what I believe is the correct format, so I am not sure what my issue is.

Example code:

const request = require('request-promise')

...

let payload =
{
     date: today,
     description: "description",
     doctor: doctorID,
     document: "data:image/png;base64,iVBORw0KGgoAAAAN.....",
     metatags: ["tag_1", "tag_2"],
     patient: patientID,
}

return request({
     method: 'POST',
     headers: {
         'Authorization': 'Bearer ' + access_token,
         'Content-Type': 'form/multipart'
     },
     formData: JSON.stringify(payload)
})
.then((response) => {
     res.status(200).send("Success");
     return response;
})
.catch((err) => {
     res.status(400).send("Failed");
     return err;
})

bri...@drchrono.com

unread,
Apr 9, 2021, 9:37:01 AM4/9/21
to DrChrono API Developers
Here is an example in JavaScript using Fetch:

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer YOUR_ACCESS_TOKEN");

var formdata = new FormData();
formdata.append("date", "2021-04-08");
formdata.append("description", "Test Document");
formdata.append("doctor", "239104");
formdata.append("document", fileInput.files[0], "image001.png");
formdata.append("patient", "82383031");

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: formdata,
redirect: 'follow'
};

fetch("https://drchrono.com/api/documents", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

Jeffrey S

unread,
Apr 12, 2021, 9:56:56 AM4/12/21
to DrChrono API Developers
What data type is the file? Blob, DataURL, Stream, etc.?

bri...@drchrono.com

unread,
Apr 12, 2021, 9:59:26 AM4/12/21
to DrChrono API Developers
You can upload most any type of file: doc, pdf, jpeg, in this instance png. You do not need to convert the file into a blob or such before sending to our API.

Jeffrey S

unread,
Apr 13, 2021, 10:04:30 AM4/13/21
to DrChrono API Developers
Thanks for your help and patience.

I've tried sending either a file or a blob or to our server to forward the data to the API, however it is still getting a timeout error (or a 400 with the fetch library). Would you know of the way to fix this? The request succeeds in Postman but I cannot get the file to work properly.

Client:
var formData = new FormData();
formData.append("date", today);
formData.append("description", description);
formData.append("doctor", doctorID);
formData.append("document", new File([blob], fileName, {type: fileType}), fileName);
formData.append("patient", patientID);
formData.append("metatags", ["tag1", "tag2"])

let response = await fetch('/submitDocument', {
     method: 'POST',
     body: formData,
     redirect: 'follow',
     headers: {
         'Content-Type': 'multipart/form-data'
    }
})

Server - result is timeout after 60 seconds:
request({
     method: 'POST',
     headers: {
         'Authorization': 'Bearer ' + access_token,
         'Content-Type': 'multipart/form-data'
     },
     formData: req.body,
     followAllRedirects: true
})
...

Server (with fetch library) - result is 400 status:
var requestOptions = {
     method: 'POST',
     headers:
     {
         'Authorization': 'Bearer ' + access_token,
         'Content-Type': 'multipart/form-data',
     },
     body: req.body,
     redirect: 'follow'
};
fetch("https://drchrono.com/api/documents", requestOptions)
...

Jeffrey S

unread,
Apr 14, 2021, 10:15:24 AM4/14/21
to DrChrono API Developers
Well after much hair pulling I managed to figure out that I had to modify the formData file format on the backend side. Here it is if anyone is curious in the future:

let payload =
{
     patient: req.body.patient,
     metatags: JSON.stringify(req.body.metatags),
     doctor: req.body.doctor,
     date: req.body.date,
     description: req.body.description,
     document:
    {
        value: Buffer.from(req.body.document.split(',')[1], 'base64'), //req.body.document is a Data URI
        options:
        {
             filename: req.body.documentName,
             contentType: null
        }
    }
};
Reply all
Reply to author
Forward
0 new messages