saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.03k stars 105 forks source link

Connector does not support `defaultBody()`? #421

Closed boryn closed 2 months ago

boryn commented 3 months ago

Either I'm wrong, or I have just discovered, that connectors do not support defaultBody()?

We came across an API which accepts the key not over the headers, but through the POST body.

So we prepared the request classes with Method::POST and HasFormBody and added an array with the API key to defaultBody() method in the connector, but it seems that the request classes do not take into account that array from the connector.

I believe adding support for defaultBody() to the connector won't break anything (of course, one needs to remember to use proper method at the request class).

We solved it by a workaround described here: https://github.com/saloonphp/saloon/discussions/408

    public function boot(PendingRequest $pendingRequest): void
    {
        $connector = $pendingRequest->getConnector();

        $pendingRequest->setBody(new FormBodyRepository($connector->getCredentials()));
    }
Sammyjo20 commented 3 months ago

Hey @boryn

If you add the same trait/interface to the connector as well as your request you will be able to add support for defaultBody() on your connector. If the body type is "mergable" e.g JSON, then the body will be merged with the request.

Please could you share your use case more please?

chatsikm commented 2 months ago

@boryn https://docs.saloon.dev/the-basics/request-body-data/xml-body#connector-body Here is an example of a connector using the defaultBody()

Sammyjo20 commented 2 months ago

Hey @boryn I'm going to close this issue now as it looks like it has been resolved, just say if you need anything.