1

I wonder, with Magento 2, when writing REST web APIs, how would one change the response sent to the client in terms of status code, headers and cookies?

I ask this because in all sample code I've seen, I could not find a clear example where they directly manipulate, or have a say so, as to what status codes and headers are sent back to the customer.

It appears that Magento somehow decides this for you and that if you really want to make these kinds of manipulations you must use Controllers instead.

1 Answer 1

0

You can handle response of Rest API by changing interfaces and class which implemented it.

Example

     * GET for Post api
     * @param string[] $orderIds
     * @return boolean
     */
    public function execute($orderIds);

Magento will read your comment in your interface, in above example -> it will return boolean, if you want change int, string .... you can define here

And need to change return type in class which implement it also.

In case you want to return a array data you need to create another interface and define it in here. Like this

     * GET for Post api
     * @param string[] $orderIds
     * @return \Extensions\Example\Api\Data\OrderInterface[]
     */
    public function getOrderInformation($orderIds);

Hope it help!

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.