0

I am a newbie to the apigility code-connected service & was able to create a RESTful service with fetch and fetchall class method on the mapper file.

Can someone point me a good sample for insert (POST) data via REST service ?

Thank you,

Kevin

1 Answers1

1

POST is going to be used for creating a new resource typically. This means that in your request you're going to want the following headers:

Accept: application/json Content-Type: application/json

The first tells Apigility what sort of a response it is expecting. The second says that the data you'll be providing to the API will be in json format.

Apigility uses json or json+hal by default for a return and expects json for the incoming data.

When you're creating a new resource, typically you'll be persisting it in a database and as such the id of the resource will be generated by your code or database. The rest of the resource will be provided by the caller to the API. Example:

POST /api/user
{
    "username": "kevin voyce",
    "firstname": "kevin",
    "lastname":" "voyce"
}

If you do this, you should see a response of something like

405 - Method Not Allowed

The body of the error should indicate that the method has not been defined. The error message is coming from the create method in the resource. Inside this method, you'll see an argument called $data which at this point will consist of a PHP stdClass with fields matching the stuff you passed in via the JSON body.

This is where the fields part of configuring your API in Apigility comes in. If you set up the names of the fields and put validators on the fields, Apigility will make sure that the fields that are passed in conform to and are valid according to these validators before the call is made into your API. The same applies to not just POST, but PATCH and PUT as well. This means that within your methods you don't have to worry that the input hasn't been validated (as long as you correctly configured your validators).

David Stockton
  • 2,261
  • 1
  • 14
  • 20