Not sure about the error, but be sure to move the env() variables to a config file and call that instead. This will break as soon as you cache your configuration in production (env() returns null)
Jan 17, 2023
39
Level 3
Laravel Http Client - POST Request Issues
Hi Guys,
I'm trying to send a POST request to an api endpoint using Laravel's HTTP client, but I seem to be having issues sending the data.
My request is as follows:
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate, br',
'Connection' => 'keep-alive',
])->withBasicAuth(env('SEND_USERNAME'), env('SEND_KEY'))->post(env('SEND_URL'), [
'messages' => [
'body' => ‘{{ $message }}',
'to' => ‘{{ $number }}',
'from' => ‘{{ $site_name }}',
]
]);
The response I'm receiving is :
{"http_code":400,"response_code":"BAD_REQUEST","response_msg":"The messages array is empty.","data":null}
Just to note, it's saying that the "The messages array is empty" but clearly within my HTTP request above, I have a messages array present.
Can anyone see where I'm going wrong?
Level 51
ok, this works...
// password is the key...
$sms = Http::withBasicAuth($username, $password)
->post('https://rest.clicksend.com/v3/sms/send', [
'messages' => [
[
'to' => '61411111111',
'body' => 'testing send',
'source' => 'php'
]
]
])
->collect();
dd($sms);
You just needed an additional array wrap!
response...
Illuminate\Support\Collection {#742 ▼ // app/Http/Controllers/TestController.php:82
#items: array:4 [▼
"http_code" => 200
"response_code" => "SUCCESS"
"response_msg" => "Messages queued for delivery."
"data" => array:5 [▼
"total_price" => 0
"total_count" => 1
"queued_count" => 1
"messages" => array:1 [▼
...
}
1 like
Please or to participate in this conversation.