When I am using content type="application/json",I cant passing any parameters and also returns "message": "Decoding error."

When I am using content type="application/x-www-form-urlencoded",I cant passing any parameters and also returns

"message": "Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded"


1 Answer 1


Creating and using rest api in magento 2 is very easy but for that you need some startup example.Magento 2 uses token based rest api.First you need to authenticate user and get the token from magento 2.After getting token you have to pass this token to every request you performed.For example to get product details by SKU you need to first authenticate user and get token now pass this token to your header to call web service /V1/products/:sku

Here I have created example for get product by SKU using magento 2 rest api.Here is list of API that supported by magento 2. LIST OF Methods REST API MAGENTO 2

Below Example please follow

Magento 2 REST API Authentication

//Authentication rest API magento2.Please change url accordingly your url
$ch = curl_init();
$data = array("username" => "wsuser", "password" => "password123");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
$token = curl_exec($ch);
$token=  json_decode($token);                                                
Get Product By SKU REST API Magento 2

//Use above token into header
$headers = array("Authorization: Bearer $token"); 

//Please note 24-MB01 is sku

$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

$result = curl_exec($ch);
$result=  json_decode($result);
  • Really thaks sir..But where i put this query in magento folder.
    – Joshua
    Commented Oct 3, 2016 at 5:04
  • I am using rest client(Add-on) for REST API purposes...If i pass the url,header and body it returns the solution...But how i do it in PHP...Where i put this above query
    – Joshua
    Commented Oct 3, 2016 at 5:15
  • what response you are getting you have to use $result->tagname you will get result or provide the response here
    – Brad Hodge
    Commented Oct 3, 2016 at 8:42
  • I have consumer key,secret,token and token secret..how i use this in programtically
    – Joshua
    Commented Oct 3, 2016 at 10:47
  • I believe you are using any addon On that Addon you will see Authorization Tab then see the screen below screencast.com/t/FI7hA6B2
    – Brad Hodge
    Commented Oct 3, 2016 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.