saloon icon indicating copy to clipboard operation
saloon copied to clipboard

Dynamically set the HTTP method

Open binaryfire opened this issue 6 months ago • 1 comments

Hi @Sammyjo20

I'm working on an app where users need to set the HTTP method of an outgoing request in the UI (stored as settings in the db). This includes users entering custom methods since not everyone sticks to just GET/POST/PUT/PATCH/etc.

The problem is that getMethod() returns the enum instance instead of the enum's case value. And then ->value is used in the calling functions.

This makes it impossible to override getMethod() to use dynamic custom values. Would you accept a PR that changes it to something this? Then it can be easily overriden with any custom logic.

    /**
     * Get the method of the request.
     */
    public function getMethod(): string
    {
        if (! isset($this->method)) {
            throw new LogicException('Your request is missing a HTTP method. You must add a method property like [protected Method $method = Method::GET]');
        }

        return $this->method->value;
    }

Cheers

binaryfire avatar Jun 25 '25 07:06 binaryfire

Could you not just add a method to your request to set the method ?


class MyRequest extends Request
{

    protected Method $method = Method::GET;
    
    
    public function setMethod(Method $method): void
    {
        $this->method = $method
    }

}


$databaseMethod = Method::tryFrom($valueInDatabase);

$request = new MyRequest();
$request->setMethod($databaseMethod);

$connector->send($request); 

Or you could pass the method in as a constructor parameter so:

class MyRequest extends Request
{

    protected Method $method = Method::GET;

    public function __construct(Method $method)
    {
        $this->method = $method
    }
}

Either should work but please test

craigpotter avatar Oct 21 '25 00:10 craigpotter