1

I want to make all requests dynamic rather than defining Http in each function followed by httpMethod like this.

Http::post()
Http::put()
Http::delete()

what i tried.

function send($method, $url) {
   Http::withToken($token)->{$method}($url)
}

function x() {
   return $this->send('GET', 'url')
}

My code above works fine and i dont know if call a function from variable output like {$method} is best practice. but I want something similar like Guzzle.

(new Client)->request($method, $url, $options)

3 Answers 3

4

if you take a look at the source code here https://github.com/illuminate/http/blob/a28981924d318272053b87712740868d9b44899e/Client/PendingRequest.php laravel is using Guzzle. so basicly Http Client is Guzzle.

and every function like POST PUT etc call a function name send

so you can just direct call send function like this.

Http::withToken($token)
    ->send('POST', 'url', [
        'headers' => [...]
        'form_params' => [
            ...
        ]
    ])
Sign up to request clarification or add additional context in comments.

1 Comment

The 'form_params' key actually depends on the body format, make sure you were using the same key as the value of 'bodyFormat' variable, possible values: form_params, json, body, multipart github.com/illuminate/http/blob/master/Client/…
2

you need to accept that param like

function send($method,$url) {
   retrun Http::withToken($token)->{$method}($url)
}

2 Comments

I forgot to add param in my question. i;ve edited my questiion
@Hafizu add retrun in Http
0

to add user_id to an endpoint:

$response = Http::get('http://localhost:8000/user/' . $user_id);

you can replace localhost:8000 to your example.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.