Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kleninmaxim's avatar

Http in Laravel, post request

My question is: why Http not work in Laravel? When I try to do such request throw curl, it is okay

$url = 'https://api.binance.com/api/v3/order'
$method = 'POST'
$headers = [
    'X-MBX-APIKEY' => 'API_KEY_PUBLIC',
    'Content-Type' => 'application/x-www-form-urlencoded'
]
$body = 'timestamp=1640683489756&symbol=BTCUSDT&type=MARKET&side=BUY&quantity=1&signature=73gvd7c53f182160ff0be23cdswe697d6278f2d6fca09drvrdf6'

But if I will do it in Laravel throw Http it breaks down:

use Illuminate\Support\Facades\Http;

Http::withHeaders([
        'X-MBX-APIKEY' => 'API_KEY_PUBLIC',
        'Content-Type' => 'application/x-www-form-urlencoded',
])->post(
        'https://api.binance.com/api/v3/order',
        [
            'timestamp' => 1640683489756,
            'symbol' => 'BTCUSDT',
            'type' => 'MARKET',
            'side' => 'BUY',
            'quantity' => 1,
            'signature' => '73gvd7c53f182160ff0be23cdswe697d6278f2d6fca09drvrdf6'
	    ]
)->collect()->toArray();

Error message is:

Array
(
    [code] => -1102
    [msg] => Mandatory parameter 'symbol' was not sent, was empty/null, or malformed.
)

timestamp and signature are okay, if they are broken I will get another message.

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try sending the data as a body https://laravel.com/docs/8.x/http-client#sending-a-raw-request-body

Http::withHeaders([
        'X-MBX-APIKEY' => 'API_KEY_PUBLIC',
        'Content-Type' => 'application/x-www-form-urlencoded',
])->withBody(http_build_query([
            'timestamp' => 1640683489756,
            'symbol' => 'BTCUSDT',
            'type' => 'MARKET',
            'side' => 'BUY',
            'quantity' => 1,
            'signature' => '73gvd7c53f182160ff0be23cdswe697d6278f2d6fca09drvrdf6'
	    ]), 'application/json')->post(
        'https://api.binance.com/api/v3/order'
)->collect()->toArray();
1 like
kleninmaxim's avatar

@Sinnbeck Thanks! Only you need to pass a string first argument instead of an array withBody:

withBody(
'timestamp=1640683489756&symbol=BTCUSDT&type=MARKET&side=BUY&quantity=1&signature=73gvd7c53f182160ff0be23cdswe697d6278f2d6fca09drvrdf6', 
'application/json'
)
->post('https://api.binance.com/api/v3/order')->

Sinnbeck's avatar

@Klenin Ah. You can probably have php convert it for you with http_build_query. I have updated my example to use that.

Please or to participate in this conversation.