Skip to content

Commit

Permalink
Add Accept and Content-Type JSON headers to Enterprise Search requests
Browse files Browse the repository at this point in the history
- Without the Accept header, Enterprise Search APIs will kick back a CSRF error
- Without the Content-Type header, APIs will not load JSON bodies as parameters per Ruby on Rails docs
  • Loading branch information
cee-chen committed Sep 9, 2020
1 parent 5b84f79 commit a21f5d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/enterprise_search/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export const WORKPLACE_SEARCH_PLUGIN = {

export const LICENSED_SUPPORT_URL = 'https://support.elastic.co';

export const JSON_HEADER = { 'Content-Type': 'application/json' }; // This needs specific casing or Chrome throws a 415 error
export const JSON_HEADER = {
'Content-Type': 'application/json', // This needs specific casing or Chrome throws a 415 error
Accept: 'application/json', // Required for Enterprise Search APIs
};

export const ENGINES_PAGE_SIZE = 10;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { mockConfig, mockLogger } from '../__mocks__';
import { JSON_HEADER } from '../../common/constants';

import { EnterpriseSearchRequestHandler } from './enterprise_search_request_handler';

Expand Down Expand Up @@ -193,7 +194,7 @@ const makeAPICall = (handler: Function, params = {}) => {
const EnterpriseSearchAPI = {
shouldHaveBeenCalledWith(expectedUrl: string, expectedParams = {}) {
expect(fetchMock).toHaveBeenCalledWith(expectedUrl, {
headers: { Authorization: 'Basic 123' },
headers: { Authorization: 'Basic 123', ...JSON_HEADER },
method: 'GET',
body: undefined,
...expectedParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Logger,
} from 'src/core/server';
import { ConfigType } from '../index';
import { JSON_HEADER } from '../../common/constants';

interface IConstructorDependencies {
config: ConfigType;
Expand Down Expand Up @@ -65,7 +66,7 @@ export class EnterpriseSearchRequestHandler {

// Set up API options
const { method } = request.route;
const headers = { Authorization: request.headers.authorization as string };
const headers = { Authorization: request.headers.authorization as string, ...JSON_HEADER };
const body = !this.isEmptyObj(request.body as object)
? JSON.stringify(request.body)
: undefined;
Expand Down

0 comments on commit a21f5d5

Please sign in to comment.