I use api-platform without ORM. This is my third API.
The 2 others are linked to a DB via Doctrine.
This one is used to link the two others, so no need of a DB.
I can't use search-filter with a GET request because I have the error:
Call to a member function getClassMetadata() on null
(because I don't map the object with an ORM...)
So I use a POST request, giving me a 201 HTTP return code.
Can I get a 200 rather than this 201 (to satisfy the CORS...)?
My Answer:
Yes, you can customize the response code returned by api-platform for a POST request. You can do this by creating a custom controller for the resource and manually setting the response code to 200. Here is an example of how you can achieve this:
php
// src/Controller/CustomController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class CustomController
{
public function customAction(): Response
{
// Your custom logic here
return new Response(null, Response::HTTP_OK);
}
}
Then, you can configure api-platform to use this custom controller for the resource by adding the following annotation to the resource class:
php
// src/Entity/YourEntity.php
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\CustomController;
/**
* @ApiResource(
* collectionOperations={
* "custom_action"={
* "method"="POST",
* "path"="/your_entity/custom_action",
* "controller"=CustomController::class,
* "status"=200
* }
* }
* )
*/
class YourEntity
{
// Your entity properties and methods
}
With this setup, when you make a POST request to the custom action endpoint, api-platform will return a 200 response code instead of the default 201.
Rate this post
5 of 5 based on 2552 votesComments