Magento 2 : Can the REST API return plain text?
Question
I'm trying to make an endpoint for the Magento 2 Web API that simply returns plain text. Is this possible and if so, how?
I'm currently trying this:
webapi.xml
:
<route url="/V1/export/txt" method="GET">
<service class="Vendor\Module\Api\ExportManagementInterface"
method="getListForExportInTxtFormat"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
Method implementation:
/**
* @return string
*/
public function getListForExportInTxtFormat(): string
{
$lines = [];
$items = $this->getListForExport();
foreach ($items->getItems() as $item) {
try {
$lines[] = $this->createLine(
$item['status'],
$item['sku']
);
} catch (\Exception $exception) {
// Not a valid status probably
}
}
return implode("\n", $lines);
}
Now when I use this request, the output is wrapped in a single <response>
XML node:
<?xml version="1.0"?>
<response>O; CBAA80511
O; CBAA80571
A; CBAA80570</response>
Is there a way that the REST API call can just return the plain TXT, without the wrapping <response>
-node? Like this:
O; CBAA80511
O; CBAA80571
A; CBAA80570
I'm using Magento 2.1.2
Solution
In your request to the API, you can specify your requested response format using the HTTP header:
Accept: application/xml
Unfortunately the only 2 MIME types Magento supports currently are application/xml
and application/json
.
The following 2 classes provide the renderer for these formats:
Magento\Framework\Webapi\Rest\Response\Renderer\Json
Magento\Framework\Webapi\Rest\Response\Renderer\Xml
You can see that both of these classes implement the interface: Magento\Framework\Webapi\Rest\Response\RendererInterface
.
You could look into creating your own plain text renderer class and using that to output your response.
OTHER TIPS
There are 2 RFCs which dictate the validity of JSON format. RFC7159 - The JavaScript Object Notation (JSON) Data Interchange Format This document states that:
JSON-text = ws value ws
So, "response" is a valid JSON object according to this RFC
There is also
RFC4627 - The application/json Media Type for JavaScript Object Notation (JSON) This document states that:
A JSON text is a serialized object or array.
JSON-text = object / array
So, "response" is NOT a valid JSON object according to this RFC]
Thus now there is a problem that using Web API from JavaScript (say, using UI components) we can't specify media type "application/json" for responses which don't represent object/array.
Currently we are discussing possibility to return different data formats depending on custom 'Accept' header provided by client. So client specifies the RFC it expects. That will allow to provide support for all clients without Backward Incompatibilities.
For now you could consider that Magento supports RFC7159 (which is newer than RFC4627) and
This document updates [RFC4627], which describes JSON and registers the media type "application/json".
Simple, easy hack :
In your controller (the method you have given in your webapi.xml 's service tag), end it with
die('string you want to return');
That way, it will bypass Magento's wrapper.
This is kind of a hack, might be seen as dirty, but is simple and works for our project.