'how can i send http post requests from Laravel to Strapi CMS via Strapi's API

I tried sending post requests to the API in postman and it works fine. but i can't find a proper syntax of doing from Laravel, i tried following this tutorial from the Laravel documentation Http-client so i tried this syntax

function sendRequest (Request $data) {
    $phonenumber = $data['number'];
    $name = $data['name'];

    $data2['data']['attributes'] = [
        [
            'phonenumber' => $phonenumber,
            'name' => $name
        ]
    ];
    
    Http::post('http://example.com/users', $data2);

}

didn't work.

here is the API's data structure

json

thanks in advance



Solution 1:[1]

I do not know if this will make a difference but you're missing a comma after value2 as per the syntax.

$response = Http::post('http://example.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator', ]);

I'm rather new to laravel also and id love to know this fix too incase I need it in the future :D good luck!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fxfey