{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/98","repository_url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch","labels_url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/98/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/98/comments","events_url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/98/events","html_url":"https://github.com/yiisoft/yii2-elasticsearch/issues/98","id":173511470,"number":98,"title":"Error creating records on Amazon Web Services","user":{"login":"reol-nbessette","id":20404101,"avatar_url":"https://avatars.githubusercontent.com/u/20404101?v=3","gravatar_id":"","url":"https://api.github.com/users/reol-nbessette","html_url":"https://github.com/reol-nbessette","followers_url":"https://api.github.com/users/reol-nbessette/followers","following_url":"https://api.github.com/users/reol-nbessette/following{/other_user}","gists_url":"https://api.github.com/users/reol-nbessette/gists{/gist_id}","starred_url":"https://api.github.com/users/reol-nbessette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reol-nbessette/subscriptions","organizations_url":"https://api.github.com/users/reol-nbessette/orgs","repos_url":"https://api.github.com/users/reol-nbessette/repos","events_url":"https://api.github.com/users/reol-nbessette/events{/privacy}","received_events_url":"https://api.github.com/users/reol-nbessette/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-08-26T18:12:09Z","updated_at":"2016-10-04T12:34:40Z","closed_at":null,"body":"### What steps will reproduce the problem?\r\n\r\nUse Amazon Web Services' ElasticSearch service. Attempt to POST a new record to an index.\r\n\r\n```php\r\n/**\r\n * @property string $name\r\n */\r\nclass User extends \\yii\\elasticsearch\\ActiveRecord\r\n{\r\n    public static function createIndex()\r\n    {\r\n        return static::getDb()->createCommand()->createIndex(static::index(), [\r\n            'mappings' => static::mapping(),\r\n        ]);\r\n    }\r\n\r\n    public static function mapping()\r\n    {\r\n        return [\r\n            static::type() => [\r\n                'properties' => [\r\n                    'name' => ['type' => 'string'],\r\n                ],\r\n            ],\r\n        ];\r\n    }\r\n\r\n    /**\r\n     * Override to remove 'op_type' => 'create' default in parent's $options.\r\n     *\r\n     * a) should not be needed\r\n     * b) this is the wrong workaround. request should set Content-Type:\r\n     *    application/json but this extension of vendor code is easier\r\n     */\r\n    //public function insert($runValidation = true, $attributes = null, $options = [])\r\n    //{\r\n    //    return parent::insert($runValidation, $attributes, $options);\r\n    //}\r\n}\r\n\r\nUser::createIndex();\r\n\r\n$user = new User(['name' => 'John Doe']);\r\n$user->save();\r\n```\r\n\r\n### What's expected?\r\n\r\nWith the code above as-is, the new record should be created/inserted/saved successfully.\r\n\r\n### What do you get instead?\r\n\r\nAWS rejects the request with the following 403 Forbidden message:\r\n\r\n> When Content-Type:application/x-www-form-urlencoded, URL cannot include query-string parameters (after '?'): '/users/user?op_type=create'\r\n\r\n### Additional info\r\n\r\nThe underlying ElasticSearch product itself allows POST requests with `Content-Type: application/x-www-form-urlencoded` and a JSON request body. While factually invalid (you can't parse a JSON request body using an x-www-form-urlencoded parser), ElasticSearch lets you bastardize the HTTP protocol.\r\n\r\nAWS does not permit this. The POST to create a new record is passing a JSON request body, and should be setting `Content-Type: application/json`. The actual method causing this problem is `\\yii\\elasticsearch\\Connection::httpRequest()` which never sets a Content-Type, even though callers are executing this method with `$requestBody` containing a JSON-encoded body.\r\n\r\nI have tested that AWS accepts the request if I manually hack the vendor `httpRequest()` method to set the correct Content-Type. Can't fix with one line though, as all JSON requests need to set the Content-Type, while non-JSON requests must remain untouched. The official ElasticSearch product should also not have a problem with this, as all you are doing is setting the proper Content-Type for requests which should require it to begin with.\r\n\r\nFor now, I am hacking around by overriding the `\\yii\\elasticsearch\\ActiveRecord::insert()` method to remove the `op_type=create` query string, as seen in the commented method in my `User` class above. The proper solution requires making the `httpRequest()` method content-type aware, so as to set `Content-Type: application/json` when appropriate.\r\n\r\n| Q                | A\r\n| ---------------- | ---\r\n| Yii vesion       | 2.0.9\r\n| yii2-elasticsearch version | 2.0.4\r\n| PHP version   5.6.24   |\r\n| Operating system | Ubuntu 16.04 LTS\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/comments/251375101","html_url":"https://github.com/yiisoft/yii2-elasticsearch/issues/98#issuecomment-251375101","issue_url":"https://api.github.com/repos/yiisoft/yii2-elasticsearch/issues/98","id":251375101,"user":{"login":"beowulfenator","id":5413344,"avatar_url":"https://avatars.githubusercontent.com/u/5413344?v=3","gravatar_id":"","url":"https://api.github.com/users/beowulfenator","html_url":"https://github.com/beowulfenator","followers_url":"https://api.github.com/users/beowulfenator/followers","following_url":"https://api.github.com/users/beowulfenator/following{/other_user}","gists_url":"https://api.github.com/users/beowulfenator/gists{/gist_id}","starred_url":"https://api.github.com/users/beowulfenator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beowulfenator/subscriptions","organizations_url":"https://api.github.com/users/beowulfenator/orgs","repos_url":"https://api.github.com/users/beowulfenator/repos","events_url":"https://api.github.com/users/beowulfenator/events{/privacy}","received_events_url":"https://api.github.com/users/beowulfenator/received_events","type":"User","site_admin":false},"created_at":"2016-10-04T12:34:39Z","updated_at":"2016-10-04T12:34:39Z","body":"Is there any other content type that allows query string parameters, yet doesn't explicitly state that it's json?"}}
{"actor":{"display_login":"beowulfenator"}}