Magento – Magento2 REST api authentication returns ‘Decoding error’

apirest

I am trying to authenticate via token based authentication. I am using 'Advanced REST client' extension in chrome to test the API.

this is my request

http://local.nomo/index.php/rest/V1/integration/admin/token
and my headers

Content-Type: application/json
Cookie: PHPSESSID=r9p1tjg450ogd6ime05jsleal0
Content-Length: 39
Source message

POST /index.php/rest/V1/integration/admin/token HTTP/1.1
 HOST: local.nomo
 content-type: application/json
 cookie: PHPSESSID=r9p1tjg450ogd6ime05jsleal0
 content-length: 39

 username='test'&password='test'

I always get this error on in response:

{
"message": "Decoding error: Decoding failed: Syntax error #0 /var/www/nomo/vendor/magento/framework/Json/Decoder.php(20): Zend_Json::decode('username='test'...') #1 /var/www/nomo/vendor/magento/framework/Webapi/Rest/Request/Deserializer/Json.php(49): Magento\Framework\Json\Decoder->decode('username='test'...') #2 /var/www/nomo/vendor/magento/framework/Webapi/Rest/Request.php(131): Magento\Framework\Webapi\Rest\Request\Deserializer\Json->deserialize('username='test'...') #3 /var/www/nomo/vendor/magento/framework/Webapi/Rest/Request.php(190): Magento\Framework\Webapi\Rest\Request->getBodyParams() #4 /var/www/nomo/var/generation/Magento/Framework/Webapi/Rest/Request/Proxy.php(127): Magento\Framework\Webapi\Rest\Request->getRequestData() #5 /var/www/nomo/vendor/magento/module-webapi/Controller/Rest.php(257): Magento\Framework\Webapi\Rest\Request\Proxy->getRequestData() #6 /var/www/nomo/vendor/magento/module-webapi/Controller/Rest.php(160): Magento\Webapi\Controller\Rest->processApiRequest() #7 /var/www/nomo/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http)) #8 /var/www/nomo/vendor/magento/framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http)) #9 /var/www/nomo/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() #10 /var/www/nomo/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) #11 {main}"
"trace": null
}

Does somebody know what is going wrong ?

Best Answer

Body should be in json format, like

{"username":"admin", "password":"123123q"}

example

POST /rest/V1/integration/admin/token HTTP/1.1 
Host: mage.dev 
Content-Type: application/json 
Accept: application/json 
Cache-Control: no-cache 

{"username":"admin", "password":"123123q"}