Skip to content

Commit a21f5d5

Browse files
committed
Add Accept and Content-Type JSON headers to Enterprise Search requests
- 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
1 parent 5b84f79 commit a21f5d5

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

x-pack/plugins/enterprise_search/common/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const WORKPLACE_SEARCH_PLUGIN = {
7070

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

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

7578
export const ENGINES_PAGE_SIZE = 10;

x-pack/plugins/enterprise_search/server/lib/enterprise_search_request_handler.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { mockConfig, mockLogger } from '../__mocks__';
8+
import { JSON_HEADER } from '../../common/constants';
89

910
import { EnterpriseSearchRequestHandler } from './enterprise_search_request_handler';
1011

@@ -193,7 +194,7 @@ const makeAPICall = (handler: Function, params = {}) => {
193194
const EnterpriseSearchAPI = {
194195
shouldHaveBeenCalledWith(expectedUrl: string, expectedParams = {}) {
195196
expect(fetchMock).toHaveBeenCalledWith(expectedUrl, {
196-
headers: { Authorization: 'Basic 123' },
197+
headers: { Authorization: 'Basic 123', ...JSON_HEADER },
197198
method: 'GET',
198199
body: undefined,
199200
...expectedParams,

x-pack/plugins/enterprise_search/server/lib/enterprise_search_request_handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Logger,
1515
} from 'src/core/server';
1616
import { ConfigType } from '../index';
17+
import { JSON_HEADER } from '../../common/constants';
1718

1819
interface IConstructorDependencies {
1920
config: ConfigType;
@@ -65,7 +66,7 @@ export class EnterpriseSearchRequestHandler {
6566

6667
// Set up API options
6768
const { method } = request.route;
68-
const headers = { Authorization: request.headers.authorization as string };
69+
const headers = { Authorization: request.headers.authorization as string, ...JSON_HEADER };
6970
const body = !this.isEmptyObj(request.body as object)
7071
? JSON.stringify(request.body)
7172
: undefined;

0 commit comments

Comments
 (0)