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

Set content-type header to application/json #7966

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core_plugins/console/public/src/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ module.exports.send = function (method, path, data, server, disable_auth_alert)
// delayed loading for circular references
var settings = require("./settings");

let contentType;
if (data) {
try {
JSON.parse(data);
contentType = 'application/json';
Copy link
Contributor Author

@ycombinator ycombinator Aug 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spalger There is (sort of) an edge case with this check. Consider the following valid _bulk API request:

POST /foo/bar/_bulk
{ "delete": { "_id": "1" } }

The content-type for this request will be set to application/json, not text/plain as it would be for multiline _bulk requests.

I'm okay with this because the body is infact valid JSON in this case. Also, Elasticsearch handles this request with its content type as application/json. Still, I wanted to point it out in case you feel differently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another edge case is when users are not using the bulk API but submit slightly invalid JSON (like { name: 'val' }). Elasticsearch will accept it, but a proxy or third party would likely fail to parse it.

Ultimately though elasticsearch doesn't consider the content-type at all, which is why we didn't bother with this to begin with. The only time we should set send Content-Type: application/json is when we think the body is in fact valid JSON, so this works for me.

} catch (e) {
contentType = 'text/plain';
}
}

var options = {
url: '../api/console/proxy?uri=' + encodeURIComponent(path),
data: method == "GET" ? null : data,
contentType,
cache: false,
crossDomain: true,
type: method,
Expand Down