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

user_hkcl's avatar

Laravel sending json data using guzzle

Hi everyone i want to send json format data usimg guzzle 6.3 but its not working..don't understand why? please suggest.

$reqParamArray = array();
    $reqParamArray['ApplicationCode'] = $ApplicationCode;
    $reqParamArray['ServiceCode'] = $ServiceCode;
    $reqParamArray['FileReferenceNo'] = $FileReferenceNo;
    $reqParamArray['ReceiptDate'] = $ReceiptDate;
    $reqParamArray['Name'] = $Name;
    $reqParamArray['MobileNo'] = $MobileNo;
    $reqParamArray['RTSDueDate'] = $RTSDueDate;
    $reqParamArray['DistrictCode'] = $DistrictCode;
    $reqParamArray['LocationType'] = $LocationType;
    $reqParamArray['LocationName'] = $LocationName;
    $reqParamArray['SourceCode'] = $SourceCode;
    $reqParamArray['LastStatusDescription'] = $LastStatusDescription;
    $reqParamArray['LastAction'] = $LastAction;
    $reqParamArray['LastActionBy'] = $LastActionBy;
    $reqParamArray['LastActionDate'] = $LastActionDate;
    $reqParamArray['Remarks_Eng'] = $Remarks_Eng;
    $reqParamArray['Level'] = $Level;
    $reqParamArray['FileWithUser'] = $FileWithUser;
    $reqParamArray['DeptCode'] = $DeptCode;

    $params[] = $reqParamArray;
    $data = json_encode($params);

    $client = new Client([
        'headers' => ['Content-Type' => 'application/json']
    ]);
    $response = $client->post('url', 
            ['body' => $data]
    );
    $response = json_decode($response->getBody(), true);
0 likes
2 replies
Yorki's avatar

You might try sending this as json

$response = $client->post('url', [
    'json' => $params,
]);
user_hkcl's avatar
user_hkcl
OP
Best Answer
Level 1

Thanks @Yorki but i have to send json encoded data .for me this works as expected

$data = json_encode($params);

$client = new Client([
    'headers' => ['Content-Type' => 'application/json']
]);
$response = $client->post('url', 
        ['body' => $data]
);
$response = json_decode($response->getBody(), true);
2 likes

Please or to participate in this conversation.