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

Leff7's avatar
Level 4

Sending a form-data post request with guzzle http

I am using guzzle http, and would like to implement the post request that looks like this when done with js:

               var formData = JSON.stringify( { id: formID, fields: fields, settings: settings, extra: extra } );
            var data = {
                'action': 'nf_ajax_submit',
                'security': nfFrontEnd.ajaxNonce,
                'formData': formData
            }

            var that = this;

            jQuery.ajax({
                url: nfFrontEnd.adminAjax,
                type: 'POST',
                data: data,
                cache: false,
                success: function( data, textStatus, jqXHR ) {
                    try {
                        var response = jQuery.parseJSON( data );
                        nfRadio.channel( 'forms' ).trigger( 'submit:response', response, textStatus, jqXHR, formModel.get( 'id' ) );
                        nfRadio.channel( 'form-' + formModel.get( 'id' ) ).trigger( 'submit:response', response, textStatus, jqXHR );
                        jQuery( document ).trigger( 'nfFormSubmitResponse', { response: response, id: formModel.get( 'id' ) } );
                    } catch( e ) {
                        console.log( e );
                        console.log( 'Parse Error' );
                        console.log( e );
                    }

                },

How can I make a post request form-data with guzzle, I have tried with doing the same thing like this:

        $url = 'http://ytf.app/wp-admin/admin-ajax.php';
        $data = [];
        $data['security'] = $this->getNonce();
        $data['action'] = 'nf_ajax_submit';
        $data['formData'] = $request->all();
        $formData = json_encode($data);

        $client   = new Client();
        $response = $client->request('POST', $url, [
            'body' =>  $formData,
        ]);

        $body = json_decode($response->getBody(), true);

        if ($body['success'] === true && $body['data'] !== false) {
            return $body['data'];
        }

But, that is not working, how should I do this?

0 likes
4 replies
andonovn's avatar
$http = new GuzzleHttp\Client;

$response = $http->post('https://your-endpoint.com', [
    'form_params' => [
         // data goes here
    ],
]);
Leff7's avatar
Level 4

But, I have tried doing that like so:

$response = $http->post('https://your-endpoint.com', [
    'form_params' => [
        'data' =>  $formData,
    ],
]);

And it didn't work, but when I have made request in the postman, where I have checked the form-data and sent the key value pairs for form fields in the body , then I got a message that form was submitted sucessfully.

marklauyq's avatar

Have you tried this

$response = $http->post('https://your-endpoint.com', [
    'form_params' => [
        'key' =>  'value',
    ],
])

or in your case

$response = $http->post('https://your-endpoint.com', [
    'form_params' =>  $formData
])
1 like

Please or to participate in this conversation.