i've tried to do some curl post to my other services using built in magento curl.

i already did this for the curl request

        $this->curl->post($url, $data);

yeah it's work but my data is change to url encoded style. my question is there any ways for me change the style from url encoded to Json Body request?

thanks in advance

3 Answers 3


This is now possible in Magento by default (I'm not sure from what version, but PR was merged in March of 2018.)

Here is an example:

/** \Magento\Framework\HTTP\ClientInterface $client */
            'Content-Type' => 'application/json'

$this->client->post($url, $jsonString);

And just as a reference, here is the PR in question: https://github.com/magento/magento2/pull/8373


Eventually it's worked using the basic Curl without using the magento curl

i am following this link https://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get

and it works beautifully


This happens when you make a curl request through magento:


protected function makeRequest($method, $uri, $params = [])
    $this->_ch = curl_init();
    $this->curlOption(CURLOPT_URL, $uri);
    if ($method == 'POST') {
        $this->curlOption(CURLOPT_POST, 1);
        $this->curlOption(CURLOPT_POSTFIELDS, http_build_query($params));
    } elseif ($method == "GET") {
        $this->curlOption(CURLOPT_HTTPGET, 1);
    } else {
        $this->curlOption(CURLOPT_CUSTOMREQUEST, $method);

http_build_query corrupts the format of the json. If you patiently read the function makeRequest(), you will be surprised that it is not possible to send json in the body or do PUT / DELETE / PATCH ... etc with params. Instead you can use the old one known

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://.../');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['a'=>'1', 'b'=>'2']));

$result = curl_exec($ch);

if ($result === false) {
    return false;
} else {
    return $result;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.