A
A
Abc Edc2015-11-18 13:59:31
Laravel
Abc Edc, 2015-11-18 13:59:31

How to send a post request with json using for example guzzle?

$params['header'] = $this->body['header'];
        $params['body'] = $this->body['body'];
     
        $api = \Illuminate\Support\Facades\Config::get('app.constants.api');
        $url = $api['base'] . $api['prefix'] . $api['version'] . '/create-news-by-admin';
        $headers = ['Content-type' => 'application/json'];
        $client = new Client();
        $request = $client->post($url, array(
            'content-type' => 'application/json'
        ), array());
        $request->setBody(json_encode($params));
        echo $url;
        try {
            $response = $request->send();
        } catch (ClientException $e) {
            echo $e->getMessage();
        }

And something to no avail

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-11-18
@gleber1

$response = $client->request('PUT', '/put', ['json' => ['foo' => 'bar']]);

docs.guzzlephp.org/en/latest/request-options.html#json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question