I want to return the JSON response suggested by the mobile app developer. My custom rest API currently returning responses in the below format.
[{
"status": true,
"data": [
{
"entity_id": "3",
"name": "Test category",
"icon_name": "test",
"category_url": "http://127.0.0.1/magento/pub/catalog/category/view/s/test-category/id/3/",
"button_name": "Sell Now"
},
{
"entity_id": "4",
"name": "magento Test",
"icon_name": null,
"category_url": "http://127.0.0.1/magento/pub/catalog/category/view/s/magento-test/id/4/",
"button_name": "Sell Now"
}
]}]
And below is the expected result:
{"status": true,
"data": [
{
"entity_id": "3",
"name": "Test category",
"icon_name": "test",
"category_url": "http://127.0.0.1/magento/pub/catalog/category/view/s/test-category/id/3/",
"button_name": "Sell Now"
},
{
"entity_id": "4",
"name": "magento Test",
"icon_name": null,
"category_url": "http://127.0.0.1/magento/pub/catalog/category/view/s/magento-test/id/4/",
"button_name": "Sell Now"
}
]
}
The code that I have implemented: Interface:
/**
* Get category collection
*
* @api
* @return string
*/
public function getCategoryCollection();
Model:
public function getCategoryCollection()
{
try
{
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addIsActiveFilter();
$i=0;
if(count($collection)){
foreach ($collection as $category){
$data[$i]=["entity_id" => $category->getId(),
"name"=> $category->getName(),
"icon_name" => $category->getData('category_icon_name'),
"category_url" => $category->getUrl(),
"button_name" => "Sell Now"
];
$i++;
}
$output = ["status"=>true, "data"=>$data];
}
else{
$output=["status"=>false,"message"=>"No Category found"];
}
return [$output];
}
catch (\Exception $exception) {
$output=["status"=> false, 'message' => $exception->getMessage()];
return [$output];
}
}
I got one solution to add the below code but using that might be not appropriate:
print_r(json_encode($output));
die();
Any help will be grateful.
-
return $output; instead of return [$output]; should do the trick.Philipp Sander– Philipp Sander2022年09月07日 08:40:50 +00:00Commented Sep 7, 2022 at 8:40
-
also please not that this is not how responses normally look like in m2Philipp Sander– Philipp Sander2022年09月07日 08:41:48 +00:00Commented Sep 7, 2022 at 8:41
-
when using only return $output; getting response like: [ true, [ { "entity_id": "3", "name": "Test category", "icon_name": "test", "category_url": "127.0.0.1/magento/pub/catalog/category/view/s/test-category/id/…", "button_name": "Sell Now" }, { "entity_id": "4", "name": "magento Test", "icon_name": null, "category_url": "127.0.0.1/magento/pub/catalog/category/view/s/magento-test/id/4", "button_name": "Sell Now" } ] ]savita nagdev– savita nagdev2022年09月07日 08:49:28 +00:00Commented Sep 7, 2022 at 8:49
-
then you have to create a response model that represents your format. as i said: you're showing the response in a format that is unusual in magento.Philipp Sander– Philipp Sander2022年09月07日 09:27:01 +00:00Commented Sep 7, 2022 at 9:27
1 Answer 1
Try sending response like this
$this->response is object of \Magento\Framework\Webapi\Rest\Response class
$this->response->setHeader('Content-Type', 'application/json', true)
->setBody(json_encode($output))
->sendResponse();
-
response is which class's object?savita nagdev– savita nagdev2022年09月07日 08:15:52 +00:00Commented Sep 7, 2022 at 8:15
-
1this should not be used in webapi controllersPhilipp Sander– Philipp Sander2022年09月07日 08:41:06 +00:00Commented Sep 7, 2022 at 8:41
-
Getting response like this: can't parse JSON. Raw result: {"status":true,"data":[{"entity_id":"3","name":"Test category","icon_name":"test","category_url":"http:\/\/127.0.0.1\/magento\/pub\/catalog\/category\/view\/s\/test-category\/id\/3\/","button_name":"Sell Now"},{"entity_id":"4","name":"magento Test","icon_name":null,"category_url":"http:\/\/127.0.0.1\/magento\/pub\/catalog\/category\/view\/s\/magento-test\/id\/4\/","button_name":"Sell Now"}]}[]savita nagdev– savita nagdev2022年09月07日 08:41:12 +00:00Commented Sep 7, 2022 at 8:41
-
Magento\Framework\Webapi\Rest\Response Inject this response object in your class using dependency injectionMaham Mubashir– Maham Mubashir2022年09月07日 09:45:35 +00:00Commented Sep 7, 2022 at 9:45
-
We should use $jsonResponse = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON); from the controllerHoangHieu– HoangHieu2022年09月07日 11:09:46 +00:00Commented Sep 7, 2022 at 11:09
Explore related questions
See similar questions with these tags.