kit: validation: throw exception if content-type is missing

Describe the bug I get this error since upgrading to SK next-81 (also happens on 83). This possibly happens because of an API route with POST requests like so:

import fetch from 'node-fetch'


/**
 * Load data from API
 */
// Get results from a POST request
export async function post ({ headers, query, body, params, ...rest }) {
    try {
        const res = await fetch(`https://${import.meta.env.VITE_SANITY_ID}.apicdn.sanity.io/v1/data/query/production?query=${encodeURIComponent(body)}`, {
            method: 'GET',
            headers: {
                Authorization: `Bearer ${import.meta.env.VITE_SANITY_TOKEN}`,
            }
        })
        const data = await res.json()

        return {
            status: 200,
            body: data
        }
    }
     catch (error) {
        return {
            status: error.status || 500,
            body: error.message || error.text || `Can't fetch query`
        }
    }
}

Logs

Cannot read property 'split' of undefined
TypeError: Cannot read property 'split' of undefined
    at parse_body (file:///site/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.83_svelte@3.37.0+vite@2.1.5/node_modules/@sveltejs/kit/dist/ssr.js:1330:60)
    at ssr (file:///site/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.83_svelte@3.37.0+vite@2.1.5/node_modules/@sveltejs/kit/dist/ssr.js:1448:9)
    at fetch (file:///site/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.83_svelte@3.37.0+vite@2.1.5/node_modules/@sveltejs/kit/dist/ssr.js:697:30)

Information about your SvelteKit Installation:

Diagnostics
  System:
    OS: macOS 11.2.3
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 480.96 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.14.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.6 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 90.1.23.71
    Firefox: 84.0.1
    Safari: 14.0.3
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.83
    svelte: ^3.37.0 => 3.37.0
    vite: ^2.1.5 => 2.1.5

Severity Well, it kind of blocks the whole app 🙈

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (19 by maintainers)

Most upvoted comments

@Rich-Harris can you please reopen this issue and rollback the commit made yesterday? I’ve just tested my code with the new SvelteKit version (next.95) and the problem still persists. Seems like the fix which has been done yesterday has no effect and has been done in a wrong place of the code. Please, see the picture bellow. It all doesn’t make much sense to me right now, but that’s just how things are.

In case you reopen the issue I’m willing to try and put together steps to reproduce the problem. It seems to be a rather tricky one. It’s prolly gonna take me a while to put all the steps together with enough details. So, please, bear with me.

gf4d5g4d8h9t7

I just wrote a big paragraph here about what I would consider the ideal API which I then erased, because I realized it was more complicated than I was thinking it was. I think it would be nice if endpoints had access to the request body before it’s even been uncompressed, because this might be a proxy to some other endpoint elsewhere, and we don’t want to decompress that data, just send it along as-is.

Do we need three types of bodies? Entirely-raw, content-encoding-decoded, and content-type-decoded?