Skip to content

Commit

Permalink
apiconsole: allow uploads in console
Browse files Browse the repository at this point in the history
By adding an XHR to "Try it out" requests, we can make non-JSON
requests pass a CORS check.
  • Loading branch information
jordigh committed Jul 26, 2024
1 parent a9521a8 commit c9f9b70
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/client/apiconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ function initialize(appModel: AppModel) {

function requestInterceptor(request: SwaggerUI.Request) {
delete request.headers.Authorization;
const url = new URL(request.url);
// Swagger will use this request interceptor for several kinds of
// requests, such as requesting the API YAML spec from Github:
//
// Function to intercept remote definition, "Try it out",
// and OAuth 2.0 requests.
//
// https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
//
// We want to ensure that only "Try it out" requests have XHR, so
// that they pass a same origin request, even if they're not GET,
// HEAD, or OPTIONS. "Try it out" requests are the requests to the
// same origin.
if (url.origin === window.origin) {
// Without this header, unauthenticated multipart POST requests
// (i.e. file uploads) would fail in the API console. We want those
// requests to succeed.
request.headers['X-Requested-With'] = 'XMLHttpRequest';
}
return request;
}

Expand Down

0 comments on commit c9f9b70

Please sign in to comment.