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

Time:02-02

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 used this syntax

Http::post('http://example.com/users', [
    "data" => [
        "key1" => 'value1',
        "key2" => 'value2'
    ]
]);

then tried this

Http::post('http://example.com/users', [
    "key1" => 'value1',
    "key2" => 'value2'
]);

and failed in both.

here is the API's data structure

json structure

thanks in advance

CodePudding user response:

I guess you just want to send a simple post request using HTTP client to strapi api. Use this example as reference. I have shown it as per the data shown in image above.

$data['data'] = [
        [
            'id' => 1,
            'attributes' => [
                    'text1' => 'value1',
                    'text2' => 'value2',
                    'createdAt' => '2022-02-01T14:59:55.711Z',
                    'updatedAt' => '2022-02-01T14:59:55.711Z'
                ]
        ]
    ];
Http::post('http://example.com/users', $data);

these methods accept an array of data as their second argument. By default, data will be sent using the application/json content type. Http-client#request-data

CodePudding user response:

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!

  •  Tags:  
  • Related