I know how to create APIs with GET method, wondering how to create custom REST API with POST Method ??..

I have spend enough time into core module but didn't get anything from there(May be I am not understanding) so is there anybody who have some Idea about this..

How can we pass & get the confidential data from POST method in REST API ??


2 Answers 2


Do this tutorial and you will see - https://alankent.me/2015/07/24/creating-a-new-rest-web-service-in-magento-2/

Specifically follow closely where the array of float is passed through in the body of the HTTP request.


<!-- Example: curl -->
<route url="/V1/calculator/add/:num1/:num2" method="GET">
    <service class="AlanKent\CalculatorWebService\Api\CalculatorInterface" method="add"/>
        <resource ref="anonymous"/>

<!-- Example: curl -d '{"nums":[1.1,2.2,3.3]}' -H 'Content-Type: application/json' -->
<route url="/V1/calculator/sum" method="POST">
    <service class="AlanKent\CalculatorWebService\Api\CalculatorInterface" method="sum"/>
        <resource ref="anonymous"/>

At the place of method="GET" write method="POST"

Parameters accessed from url address are called as PATH parameters. By above tutorial you will get how to access these variables.

But in POST method user send the data as JSON FORMAT in request body

follow this link to know how to access POST DATA passed by client

how to retrieve the request body parameters in magento 2.0

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.