-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validation: throw exception if content-type is missing #1127
Comments
This is failing inside Vite and cannot be reproduced without seeing what you are sending with your POST request. Other notes:
|
The line where the error is being thrown is
Presumably this is crashing when you don't have a |
Hm. Although I'm also not clear on why this code is getting run at all. I don't know why |
I think we should warn if |
If I'm interpreting |
hmm. if |
thinking about it further i think we should just throw if the content type is missing, just like we throw if it's an unknown type (and |
What's the drawback of allowing unknown or missing types though? Could there be ContentTypes that are unknown to us but need to be set like this by the user because of some backend quirks? |
I think I'm confused about in what sorts of situations |
For adapters using kit/packages/kit/src/core/http/index.js Lines 53 to 62 in 1237eb3
For e.g. Netlify, it's kit/packages/adapter-netlify/files/entry.js Lines 17 to 22 in 5c2665f
For CFW, it's kit/packages/adapter-cloudflare-workers/files/entry.js Lines 55 to 62 in 5c2665f
Perhaps it would be easier if |
I think that might make more sense. That's what it really is, right? Any decoding of that to a string involves some awareness of what its encoding was, and it doesn't seem like a 'raw' body anymore. The base64 stuff in Netlify seems like somewhat of an anomaly, and if that's what their API is, I think we should be transparently handling that, but probably only decoding it back to a buffer, not to a string. |
What do we do in the Netlify adapter in the case where it's not base64 encoded — turn it into a buffer... const encoding = get_charset(headers['content-type']) || 'utf-8';
rawBody = new TextEncoder(encoding).encode(str).buffer; ...so that SvelteKit can turn it back into a string... const text = new TextDecoder(encoding).decode(new UInt8Array(rawBody)); ...or just leave it as a string? |
also, it seems the use of const decoder = new TextDecoder(h['content-encoding'] || 'utf-8');
fulfil(decoder.decode(data)); i got confused, |
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? |
man, at this point i don't have a clue |
|
@Conduitry Thnx for pointing out this for me, found another 'thing': const [type, ...directives] = req.headers['content-type'].split(/;\s*/); content-type should be lowercase letters in your header: { 'content-type': ...} |
Hm. By the time it gets to the What are you doing to call the endpoint with |
Really nothing, I created fresh project to see what was the problem. <script context="module">
export async function load({fetch}) {
const res = await fetch('/server', {
method: 'POST',
headers: {
'Content-Type': 'application/json' // on purpose like this
},
body: {
name: 'Hello world'
}
})
const { name } = await res.json()
return {
props: {
name
}
}
}
</script>
<script>
export let name
</script>
<h1>{name}, Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p> and server.js: export async function post({body}) {
return {
body: {
name: body.name
}
}
} i put this in @sveltejs/kit/dist/ssr.js to see what's going on: console.log('headers', req.headers)
const [type, ...directives] = req.headers['content-type'].split(/;\s*/); and i get this fromin the console:
so I guess it's not normalized. when i change to lowercase 'content-type' it works fine |
Alright, since I didn't specified a |
@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. |
@phaleth did you find a solution to this? I am stuck with a broken deployment to Netlify, while local dev server and adapter-node works. |
@flayks Where did you add 'content-type' into your code to make it work and what type did you use? |
I don't anymore as the content-type is by default AFAIK on the API side, but I use this for my routes requests: <script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load ({ page, fetch, session, context }) {
const res = await fetch(`/api/sanity`, {
method: 'POST',
headers: {
'content-type': 'text/plain',
},
body: `…`
})
}
... |
@bwklein This very same annoying issue was brought up with a reproduction here #1463 and closed by Rich's successful commit couple of weeks afterwards. You can go through the steps described in the issue #1463 and confirm to yourself that everything still works. In case the issue reapeared with that repro please open a new issue. Edit: Actually, someone else fixed the problem in the middle of May, but Rich should still get the credit. |
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:
Logs
Information about your SvelteKit Installation:
Diagnostics
Severity
Well, it kind of blocks the whole app 🙈
The text was updated successfully, but these errors were encountered: