Skip to content
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

How can I retrieve options data from browser's OPTIONS request #2608

Closed
GabLeRoux opened this issue Dec 18, 2019 · 4 comments
Closed

How can I retrieve options data from browser's OPTIONS request #2608

GabLeRoux opened this issue Dec 18, 2019 · 4 comments

Comments

@GabLeRoux
Copy link

GabLeRoux commented Dec 18, 2019

Is there a way to retrieve browser's OPTIONS response data when doing a GET request or do I absolutely need to call axios.options() on my own?

I plan to generate a form based on the response data sent from an API's OPTIONS response I can then generate a form dynamically. Right now, when doing OPTIONS request on my own, it works just fine, but when I do a GET request, there's an additional OPTIONS request sent.

Just asking, but it doesn't make a big difference in the end. I guess if I want to only have a single OPTIONS call, I'd have to follow #888

@chinesedfan
Copy link
Collaborator

The HTTP OPTIONS method is used to describe the communication options for the target resource.

Don't retrieve data by OPTIONS. Your additional OPTIONS request is caused by CORS. See more about CORS from MDN.

@GabLeRoux
Copy link
Author

I’m not looking for actual data from the resource, the communication description is exactly what I’m looking for. I am using django-rest-framewrok and the OPTIONS result contains the description of what can be posted, the fields, description, field types, etc. I am using this to dynamically generate a form that is used to POST or PUT to the target in a restful way as described by the api.

Right now, it works flawlessly, but there are two OPTIONS calls, the first one is the one I invoke myself because I did not figure out how I can retrieve the OPTIONS response from a simple GET request using axios.

In other words, my OPTIONS usage is perfectly fine, I’m only wondering how I can retrieve the OPTIONS response automatically sent by the browser instead of manually invoking it.

At this point in my case, it’s definitely an early optimization. It’s more a matter of knowing if it’s possible to do so.

Thanks

@chinesedfan
Copy link
Collaborator

@GabLeRoux Sorry for misunderstanding your problem. But as far as I know, the preflight OPTIONS request is send by the browser automatically and we can't retrieve response from it. Maybe you can ask in stackoverflow. It is an interesting question and I am glad to follow your question if it is somewhere.

@chinesedfan
Copy link
Collaborator

Finally, Access-Control-Max-Age gives the value in seconds for how long the response to the preflight request can be cached for without sending another preflight request. In this case, 86400 seconds is 24 hours. Note that each browser has a maximum internal value that takes precedence when the Access-Control-Max-Age is greater.

Hope it can solve your double OPTIONS problem.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants