How to avoid requesting OPTIONS options with node request package for CORS?
I just want to post a json message, but by default the request does an OPTIONS preview.
I would like to avoid this, since users often have unreliable connections, an extra request further reduces reliability and leads to cryptic error messages such as "CORS reject".
var request = require('request');
function (data, cb) {
if (!cb) cb = function () {};
request({
method: "POST",
url: "someurl",
json:true,
body: data
}, function (err, response, body) {
if (err) cb(err);
else if (response.statusCode != 200) {
cb(new Error("log satus code: " + response.statusCode));
} else {
cb(null, body);
}
})
To clarify, I'm doing real CORS and want to avoid the OPF preflight request. I also control the pitch (although that doesn't matter).
source to share
The OPTIONS preset request is a required part of the CORS flow. No about that. However, the client can cache the response to the pre-flight request, so he only needs to make the request one time before the flight, rather than every time it is sent.
To enable pre-flight request caching, the pre-check request must respond with a header Access-Control-Max-Age
. The value of this header is the number of seconds that the client is allowed to cache the response.
For example, the following response header will allow the client to cache the preview response for 5 minutes.
Access-Control-Max-Age: 300
You will need to select the value that is appropriate for your application. It is generally recommended that you set this value to something that is not too large if you need to change the preflight response in the future. If you allow the pre-check request to be cached for a month, users may not receive your changes until their cache expires after a month.
source to share
Simple inquiries do not need a pre-sales inquiry. I'm guessing this is json: true
setting a custom title Content-Type
(most likely application/json
).
Simple values โโfor Content-Type
are:
-
application/x-www-form-urlencoded
-
multipart/form-data
-
text/plain
Anything outside these values โโwill trigger a pre-flight request.
source to share