php-http / client-common

Common HTTP Client implementations and tools for HTTPlug
http://httplug.io
MIT License
1.01k stars 53 forks source link

Add default body #213

Closed vanthao03596 closed 3 years ago

vanthao03596 commented 3 years ago

Description

I want to add two field to body in every request to authentication. Which is the best way to do that ?. Can anybody help me ?

dbu commented 3 years ago

authentication in the body is unusual, most systems use a field in the header for this to avoid mixing authentication with the application logic.

if it was in the header, there would be a plugin to do that.

for the body, it is tricky to do on http client level. the body is a string, so you would have to write a plugin to parse the body (assuming it is json or maybe xml?) and add the fields, then re-serialize the body and create an updated request.

if you really need to have authentication in the body, i'd recommend that you write a decorator around the http client that does this and that offers methods like post(string $path, array $body). the $body would be enriched by your decorator with the authentication information and then serialized and only your decorator interacts with the actual HTTP client.

vanthao03596 commented 3 years ago

@dbu thank's for response, example of this usecase is Paddle api

dbu commented 3 years ago

hope it helps. i close the issue as i think there is nothing to do on the client-common library.