Hi, I create the new custom collection route with POST method.
My problem is that the answer is always 201 (created), but I don't create new item, I checking the user password with email password parameters.
{
"email": "[email protected]",
"password": "myPassword"
}
How to change the response code in custom route?
You have to return your own Response instance in your controller instead of an array or something else.
Bumping this ticket (should I open a new one ?), as POST does not always mean 201. See https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
Could open a PR if interested ?
@Taluu RFC 2616 is obsolete: https://www.mnot.net/blog/2014/06/07/rfc2616_is_dead
But yes, you're right. POST does not always return 201 as the successful status code. That said, this issue is about custom operations, so I don't think it's a problem for us.
Not only for custom operations actually, could be useful for classic operations. Such as adding a status_code or something like that on the operation's config ?
I agree with Taluu,
imagine an API route:
POST /api/objects/simulate
which returns the object with calculated fields and the id to 0 before persisting with the real URL:
POST /api/objects
it is necessary that in the first request, the status returned http is 200
(in my case, I do not use doctrine entities)
Hmm... Indeed we should support overriding the HTTP status code.
Hmm... Indeed we should support overriding the HTTP status code.
I agree with you: should be possible in custom operations to override success HTTP status code
We now have a status=202 option.
Example of usage:
when sending manifest not save it in database we want to get 200 instead 201 after POST.
src/Entity/Manifest.php
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* collectionOperations={
* "post"={
* "path"="/manifest",
* "status"=200,
* }
* },
* itemOperations={"get"={"method"="GET"}}
* )
*/
class Manifest
{
/**
* @ApiProperty(identifier=true)
* @Assert\EqualTo("uploadAnnouncement")
*/
private $type;
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
}
In official docs there are examples in yaml and xml.
https://api-platform.com/docs/core/operations/#configuring-operations
Most helpful comment
I agree with Taluu,
imagine an API route:
POST /api/objects/simulate
which returns the object with calculated fields and the id to 0 before persisting with the real URL:
POST /api/objects
it is necessary that in the first request, the status returned http is 200
(in my case, I do not use doctrine entities)