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"

  • what is your Url What you are sending in GET & POST Parameter please provide response & Body data – Brad Hodge Sep 29 '16 at 12:04
  • URL:"shop.winfashion.net/rest/V1/integration/customer/token" BODY:-H "Content-Type: application/json" -d '{"username":"myuser","password": "mypassword"}' – Joshua Sep 30 '16 at 5:27
  • To request an authentication token for an admin user for the REST web API: curl -X POST "magento.host/index.php/rest/V1/integration/admin/token" \ -H "Content-Type:application/json" \ -d '{"username":"customer@example.com", "password":"123123q"}' HOW I USE THIS.............. – Joshua Sep 30 '16 at 5:59
  • I replied with example which will work for you let me if any issue – Brad Hodge Sep 30 '16 at 8:33

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 Oct 3 '16 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 Oct 3 '16 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 Oct 3 '16 at 8:42
  • I have consumer key,secret,token and token secret..how i use this in programtically – Joshua Oct 3 '16 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 Oct 3 '16 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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