directus / v8-archive

Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API

Home Page:https://docs.directus.io/api/reference.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to fetch Request Body in Custom API?

groovyrb opened this issue · comments

#help I have Created a custom API and the POST and PATCH Method is not working. Not able to find any solution default function are not working?
Here is code Can someone tell what I am doing worng
`<?php
use Directus\Application\Application;
use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Application\Route;
use Directus\Services\ItemsService;
use Directus\Services\RevisionsService;

return [
'/data' => [
'group' => true,
'endpoints' => [
// `/custom//data
'' => [
'method' => 'POST',
'handler' => function ($request, $response) {

          print_r($request);
          
          exit;
        $itemsService = new ItemsService($this);
        $params = $request->getQueryParams();
        $responseData = $itemsService->findAll('sample', $params);
        #$sample = sample();
        #return $response->withJson($responseData);

        

      }
  ],
  // `/custom/<endpoint-id>/data/update
  '/update' => [
    'method' => 'POST',
    'handler' => function (Request $request, Response $response) {

          #$this->validateRequestPayload($request);
       // $itemsService = new ItemsService($this->container);
            $payload = $request->getParsedBody();
            // echo $this->container;
            $params = $request;

       
        $collection = 'activity';
        #$itemsService->throwErrorIfSystemTable($collection);
        
         #$id = $request->getAttribute('id');
        
          var_dump($params);
          
          exit;
         
          $responseData = $itemsService->update($collection, $id, $payload, $params);

          return $this->responseWithData($request, $response, $responseData);

    }
  ],
  // `/custom/<endpoint-id>/data/delete
  '/delete' => [
    'method' => 'DELETE',
    'handler' => function (Request $request, Response $response) {
       
         $params = $request->getQueryParams();

         return deletedata($params);

         

    }
  ]
]

]
];`

I have found the solution and issue causing it!