Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Post request in laravel using guzzle

I am trying to notify a slack channel whenever a new order appears in our site. But I am struggling to perform this CURL request in Laravel

curl -X POST --data-urlencode "payload={\"channel\": \"#new-order\", \"username\": \"product-notifier\", \"text\": \"New Orders: \n https://mission-control.airbringr.com/orders/item/411980\", \"icon_emoji\": \":smiley:\"}" https://hooks.slack.com/services/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

I’ve been trying to come up with a solution:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    public function slack(Request $request){
        $client = new \GuzzleHttp\Client();
        try {
        
            $client = new Client();
            
            $response = $client->post('https://hooks.slack.com/services/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',[
                'json' => [
                    'payload' =>
                        ['channel'=>'#new-order','username'=> 'product-notifier', 'text'=>'New Orders: \n https://mission-control.airbringr.com/orders/item/411980', 'icon_emoji'=>':smiley:']
                        ],
                        ]
                        
            );
        } catch (\Exception $e) {
            return response()->json($e->getMessage(), 400);
        }
        return response()->json(["message" => "notified successfully"], 200);
    }

But I am getting "Client error: `POST https:\/\/hooks.slack.com\/services\/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` resulted in a `400 Bad Request` response:\nmissing_text_or_fallback_or_attachments\n"

but the curl request works just fine in my terminal.

Any suggestions on how can I make this work?

>Solution :

Atleast you do not need to wrap you data in a payload key. Which you can see in the documentation here.

$response = $client->post('https://hooks.slack.com/services/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',[
    'json' => [
        'channel'=>'#new-order',
        'username'=> 'product-notifier',
        'text'=>'New Orders: \n https://mission-control.airbringr.com/orders/item/411980', 'icon_emoji'=>':smiley:'
        ]
    ],
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading