yii\httpclient\Client cannot send in JSON

Hi there,

I’m trying to send an email through an ESP APIs by means of yii\httpclient\Client

This works ($response->isOk), with the default format \yii\httpclient\Client::FORMAT_URLENCODED


$httpClient = new Client();


$request = $httpClient->createRequest()

->setUrl('XXX')

->setHeaders(['authorization'=>'Basic ' . base64_encode('XXX')])

->addHeaders(['from'=>'XXX'])

->setMethod('post')

    ->setData([

    'from' => 'XXX',

    'to' => 'XXX',

    'subject' => 'XXX',

    'text' => 'XXX',

    ]);

/*->setFormat(Client::FORMAT_JSON)

->setData([

    'from'=>'XXX',

    'to':'XXX',

    'subject':'XXX',

    'text':'XXX'

]);*/


$response = $request->send();

But this doesn’t, with format \yii\httpclient\Client::FORMAT_JSON


$httpClient = new Client();


$request = $httpClient->createRequest()

->setUrl('XXX')

->setHeaders(['authorization'=>'Basic ' . base64_encode('XXX')])

->addHeaders(['from'=>'XXX'])

/*->setMethod('post')

    ->setData([

    'from' => 'XXX',

    'to' => 'XXX',

    'subject' => 'XXX',

    'text' => 'XXX',

    ]);*/

->setFormat(Client::FORMAT_JSON)

->setData([

    'from'=>'XXX',

    'to':'XXX',

    'subject':'XXX',

    'text':'XXX'

]);

Is there anything wrong? I am getting validation issues.

Also, is there any benefit to use JSON, or is it just a matter of preference.

Nevemind, it seems that mailgun just doesn’t suppport JSON in the request’s message-body.