Response "406 unacceptable" on env.ASSETS.fetch() for YAML file

Hi all,

i am having a hard time debugging an issue while using Cloudflare pages functions. In a nutshell, at some point in a middleware I am trying to load a YAML file that is part of my pages project. However, “fetch” fails and returns a “406 Unacceptable” response. The problem, though, is that my code works perfectly when running “wrangler pages dev”, but it does not after the deployment to Cloudflare servers.

Here’s the relevant code:

const validate: PagesFunction<ENV> = async (context) => {
  const { request, env, next } = context
  const form = await loadForm(request, env)
...
}
  
async function loadForm(request: Request, env: ENV): Promise<Form> {
  const url = buildFormURL(request)
  const response = await env.ASSETS.fetch(url)
}
  
function buildFormURL(request: Request): string {
// just some logic to create the URL to a YAML file
}

Of course, I’ve double-checked that “buildFormURL()” returns the correct URL even on Cloudflare pages servers, and that the YAML file can be accessed from within a browser.

As I said, when accessing the response “response.status” has the “406” value, and “response.statusText” has the “unacceptable” value. Given the headers returned by using curl on that same url, I tried changing the fetch call to:

// build a URL instance
...
const formRequest = new Request(url.href, {
    method: 'GET',
    headers: {
      accept: 'text/yaml',
      'accept-charset': 'utf-8',
      'content-type': 'application/json',
    },
  })
  const response = await env.ASSETS.fetch(formRequest)
  ...

but with no luck: same “406 Unacceptable” response

What am I doing wrong? How can I get a middleware in Cloudflare pages functions to load a YAML asset that is part of the same pages project?

This issue is probably of interest to you:

The prod behaviour is a little weird right now. What if you try something like this (note the addition of passing through the cf property)?

const formRequest = new Request(url.href, {
    cf: request.cf // original request from the Pages Function
})
1 Like

cherryjimbo wow, first off thanks for such a prompt reply! :slight_smile:
In deed that line did the trick! I tried looking for issues in the wrangler repo, but that one didn’t pop out. Thank you so much for this solution!

1 Like

Happy to help! I ran into the exact same issue a few months ago and lost many an hour to debugging it. :sweat_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.