Cloudflare Worker / Images API - 415 : Unsupported Media Type

Hi All,

I’m trying to send an image from a Cloudflare worker to Cloudflare Images, but I keep getting a 415 : Unsupported Media Type error. I’ve tried many different things, but now, I am at a complete loss.

Web Page → CF Worker → CF Images

WHAT AM I MISSING!?

Here is my code:

fnHandlePost = async function (oReq) {
    // capture the form data
    const oFormData = await oReq.formData()
    , oFile = oFormData.get('file')
    , sName = oFormData.get('name')
    ;
    
    // setup the response
    let oImageResponse
    // create a new form to submit
    , oNewFormData = new FormData()
    ;

    // add the file to the new form
    oNewFormData.append("file", oFile, `${sName}`);
    
    try{
      // handle the file
      oImageResponse = await fetch(`https://api.cloudflare.com/client/v4/accounts/${CF_API_TOKEN}/images/v1`, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${AUTH_BEARER}`,
         // "Content-Type": 'multipart/form-data'
        },
        body: oNewFormData 
      }).then(success => {
        console.log(`${success.status} : ${success.statusText}`)  
      })
      ;
      
    }catch(e) {
      console.log(e.message)
    }
}

Any help would be appreciated. Thanks everyone!

I figured out the problem, and it’s a very interesting one that is not documented.

For context, I moved from trying to upload an image directly to uploading an image via URL (upload an image by calling the url of the image… ex: addam.com). I was still getting the same error.

I found that the image can not have any parameters after the “.png” extension ( …png?w=300&h=200). The images I was uploading were from Contentful and they had sizes as part of the image. Once I removed those, the upload worked.

Not sure if this is worthy of changing (I get it), but worth putting in the documentation. I’d love to hear if the CF team would take a look at this. Is this intentional, or is it just an edge case?

Hope this helps.
-Addam

Hi, query params should not affect upload via url

They state that the only accept certain formats.
Supported image formats and limitations

If that should be determined by the extension can sure be discussed. But I have a hunch that this might have to do with another security measure regarding caching.
Cache Deception Armor