0

We're making a project with both frontend and backend, there is auth form, which should make POST request to backend, giving JSON object with email and password of user. If I specify 'Content-Type': 'application/json', then we see 2 requests instead of one: options (first) and post (second) requests, that results an error even with running backend (screenshot provides errors without running backend, only frontend) 2 req

const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: dataArr
            });

But when I specify 'Content-Type': 'application/x-www-form-urlencoded' and body: JSON.stringify(data), then there is no options request, only post one, but the data is then passed not as json object, so backend can't resolve the request properly. It awaits for json object post request result

const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: JSON.stringify(data)
        });

Is there any way to pass data as JSON object via POST properly without OPTIONS requests? backend uses python FastAPI

2
  • This is called a "preflight check", and is actually exactly as web standards specify. Only "simple requests" can be made without a preflight check (i.e. the OPTIONS request), and a POST request can only be simple if it has a Content-Type of application/x-www-form-urlencoded, multipart/form-data, or text/plain. So there is no way to do this in a browser. Commented Dec 12, 2024 at 19:35
  • 2
    This sounds a bit like an XY problem. WHY do you want to remove the OPTIONS request, what problem are you actually trying to solve? Most likely, this is a CORS issue, where the solution is to learn how to set up CORS properly in FastAPI. Commented Dec 12, 2024 at 19:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.