-
Notifications
You must be signed in to change notification settings - Fork 88
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
Question: Is there a way to set the Connection: keep-alive #131
Comments
I'm not sure why that would happen, can you share the details of the 500 error? A HAR of the request and the debug output from Mockttp might also be useful.
Yes, you just need to put |
@pimterry
Thank you for teaching me.
I am very sorry. However, when I tried the method I was taught, I confirmed that I could communicate with safari. I'll take a look at this. |
So it's hard to know, but one thing that could be causing your Safari issue is this Node.js bug: nodejs/node#46321. Mockttp does remove the connection header, in cases like this where you override the headers and don't specify it. That header isn't actually required though, it's totally unnecessary, so this shouldn't cause problems. For HTTP/1.1+, keep alive is the default if the connection header isn't specified. The bug above though is that Node.js does this incorrectly, and treats 'close' as the default if the header isn't sent. This shouldn't usually be a big issue, but it can confuse some clients, which will expect the connection to stay open, and so when they try to send a second request they will unexpectedly fail when they discover it's closed. I wouldn't expect this to result in a 500 error, but it's the kind of thing that can create odd errors anyway. If that is the issue, explicitly adding your own |
@pimterry I understad that the node.js version that incorporates #46321 will be released around April 2023. Please ask additional questions.
I understand that to apply the default headers do not set HEADERS_OPTIONS as an argument, is this correct?
Is there any way to solve this other than setting a custom header? |
No, to handle CORS you do need to set custom headers. That's fine though! It is possible to use custom headers, you just need to include const HEADERS_OPTIONS = {
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Methods": "GET, OPTIONS, POST",
// CORS
"Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Allow-Credentials": "true",
// Enable keep-alive:
"Connection": "keep-alive" // <-- here
}; Does that make sense? |
@pimterry
Yes, i understand. I asked if there was another way to do the above. |
Oh I see, you want a different way to handle CORS? Sorry, I thought you wanted a different way to handle the default headers part, my mistake. It is possible to do CORS automatically, you can do so by passing a // Create a server with default CORS settings all enabled:
getLocal({ cors: true });
// Create a server with custom CORS configuration:
getLocal({
cors: {
origin: 'http://example.com', // Only allow this origin
credentials: true, // Allow credentials with CORS
allowedHeaders: ['a', 'b'] // Allow specific headers
}
}); This option gets passed to the cors module, so if you want to configure it you can use any option supported there. Be aware that if you do this instead of handling CORS yourself, then it will be applied to all requests to this server, and you won't be able to mock OPTIONS requests manually. If you just want to receive everything that's fine, but if you want more precise control you may want to mock requests by hand instead, as you've been doing. |
@pimterry I see, set it to the cors property of getLocal(), right? I didn't know how to set the CorsOptions, so it was very helpful. When I took it in as a trial and checked it, I confirmed that the error was resolved. Thank you for your instruction! |
Hi!
I'm using nuxt and trying to http communicate using mockttp.
The environment is as follows.
Client side source is as follows.
Server source with mockttp is as follows.
I started the cliend and server and pressed submit button.
The network results are as follows.
This is the result in chrome, but when I do it in safari I get a 500 error.
One thing that annoys me is that Connection: keep-alive is not set in the response.
I have a question, is it possible to set Connection: keep-alive with mockttp?
Thank you.
The text was updated successfully, but these errors were encountered: