I'm trying to create a POST call to a custom API for magento 2 , but I do not know how to do this. What is the correct way to do this request?



<?xml version="1.0"?>
<routes xmlns:xsi="http://www.ws3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/rest/produtos/" method="POST">
        <service class="Api\Rest\Api\HelloInterface" method="name"/>
            <resource ref="anonymous"/>


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Api\Rest\Api\HelloInterface" type="Api\Rest\Model\Hello" />


namespace Api\Rest\Api;

interface HelloInterface

public function name($nome);



namespace Api\Rest\Model;
use Api\Rest\Api\HelloInterface;
class Hello implements HelloInterface

    protected $request;
    public function __construct(
        \Magento\Framework\App\RequestInterface $request
        $this->_request = $request;

    public function name($nome)
        //$params = $this->_request->getParams();
        return "test";


My request


Content-Type: application/json



Return Body:

"message": "\"%fieldName\" is required. Enter and try again.",
"parameters": {
    "fieldName": "nome"
} }
  • postman is your friend – Philipp Sander May 8 at 13:35
  • Yes! Body: { "message": "\"%fieldName\" is required. Enter and try again.", "parameters": { "fieldName": "nome" } } – Luis Faconi May 8 at 13:36
  • you can remove $nome parameter from function public function name($nome). try with without parameter like as public function name() – Shailesh Katarmal May 8 at 14:01
  • how do you send {"nome":"test"} ? and where is your setter and getter ? – fmsthird May 8 at 14:12
  • 1
    it worked, thank you! But is there any way to get json? – Luis Faconi May 8 at 14:24

Your Answer

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

Browse other questions tagged or ask your own question.