Cloudconvert - Invalid signature error when using request-promise to upload via API

734 views Asked by At

I'm attempting to upload a pdf through the cloudconvert API using nodeJS and request-promise. The request to get the upload URL and parameters is successful, but when I attempt to pass the data I get a 401 - "FormPost: Invalid Signature" error, even though I'm using the signature returned from the first request.

...
pdfToPng: function(pdfBuffer, apiKey) {
    return new Promise(async (resolve, reject) => {
      const cloudConvert = new CloudConvert(apiKey);
      let response = JSON.parse(
        await request.post('https://api.cloudconvert.com/v2/import/upload', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${apiKey}`,
          'Content-type': 'application/json',
        },
      }));
      let fileUrl = response.data.result.form.url;
      let params = response.data.result.form.parameters;
      let fileUpload = await request.post({
        url: fileUrl,
        formData: {
          "expires": params.expires,
          "max_file_count": params.max_file_count,
          "max_file_size": params.max_file_size,
          "signature": params.signature,
          "file": {
            value: pdfBuffer.toString('base64'),
            options: {
              filename: 'invoice.pdf',
              contentType: 'application/pdf'
            }
          }
        }
      }).catch((err) => {
        console.log('ERROR UPLOADING FILE: ', err); //<-- 401 Error here
        reject(err);
      })
      console.log('FILE UPLOAD RESPONSE: ', fileUpload);
...
1

There are 1 answers

0
monday On BEST ANSWER

You need to pass all parameters of response.data.result.form.parameters to formData, not just the named ones:

  let fileUpload = await request.post({
    url: fileUrl,
    formData: {
      ...response.data.result.form.parameters,
      "file": {
        value: pdfBuffer.toString('base64'),
        options: {
          filename: 'invoice.pdf',
          contentType: 'application/pdf'
        }
      }
    }