Route requests through local proxy with CORS#95
Open
u8sand wants to merge 4 commits into
Open
Conversation
|
Thanks to you, I was able to resolve the error! |
|
I did notice one problem. In the swagger preview, the request is made as a POST, but when I checked the API server log, I noticed that the request was rewritten as a GET (probably by a script that avoids cors). So anything other than GET requests won't work properly. |
Author
|
This should be an easy fix, i can do it later today. Thanks for reporting. Edit: I've updated it, the PR comment, merged with upstream and re-released. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because VSCode has no intention of changing their stance on required CORS headers to work in webviews microsoft/vscode#72900 -- the only workaround I see is using a proxy service like http://www.whateverorigin.org/ which just proxies requests and adds CORS headers.
Fortunately this app is in a unique place in that it's already running a webserver. So we can use that same server to proxy requests and add CORS. This is pretty easy with SwaggerUIBundle's
requestInterceptorconfig argument which takes a function for altering requests.Thus this PR does two things:
/proxyendpoint to the server which takes a query paramurlwith the actual URL. userequestlibrary to forward the entire request object to that url. Add CORS to the response and return it.requestInterceptorargument toSwaggerUIBundleto change the url to use the backend's/proxy?url=...The result -- no need to worry about CORS, the UI works the same.
Fixes #30
Fixes #23
Edit: Since this has been here for a while; if anyone is interested in using this now it can be done with the release on my fork where I have included the compiled vsix file: https://github.com/MaayanLab/vs-swagger-viewer/releases if you do use it, let me know if you find any issues.
Edit: Updated a bit to fix an issue with POST requests
requestas a dependency, using insteadhttpandhttpsurlwhich may also cause conflicts, we alwaysPOSTto/proxyserializing the url, method, headers, and body with jsonEdit: A bug in the last fix broke querystrings 🤦 this was fixed.