Siddiqui
Siddiqui

Reputation: 113

Sending POST request with Guzzle in Laravel

I'm trying to use ZohoMail's API to send email through my application. But it keeps giving me:

"{errorCode":"INVALID_METHOD"},"status":{"code":404,"description":"Invalid Input"}}

Here's the link to the Call that I'm trying to make: https://www.zoho.com/mail/help/api/post-send-an-email.html#Request_Body

Here's my function:

public static function sendEmail ($AccountId, $AuthCode, $FromAddress, $ToAddress, $Subject, $Content){

    $client = new Client(); //GuzzleHttp\Client
    $URI = 'http://mail.zoho.com/api/accounts/' . $AccountId . '/messages';
    $headers = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
    $body = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
    $Nbody = json_encode($body);
    $response = $client->post($URI, $headers, $Nbody);
    echo "DONE!";

}

I've tried changing the way I'm making the call but it doesn't seem like that's the problem. I've tested the call in PostMan and it works fine so there is probably something wrong with the way I'm making the call. Any help would be much appreciated.

Upvotes: 4

Views: 44414

Answers (6)

ederrafo
ederrafo

Reputation: 71

This works anywhere place


use GuzzleHttp\Client;

$client   = new Client();
$options = [];
$options['form_params'] = $data;
$options['http_errors'] = false; // for get exception y api response
$options['timeout'] = 5; // milliseconds

$client->request('PUT', $uri , $options);

Upvotes: 0

Niclausel
Niclausel

Reputation: 139

 $this->clients = new Client(['base_uri' => 'Url', 'timeout'  => 2.0]);
    $params['headers'] = ['Content-Type' => 'application/json'];
    $params['json'] = array(
      'parama1'=>$req->parama1,
      'parama1'=>$req->parama2,
      'parama3'=>$req->parama3,
    );
    $response = $this->clients->get('SearchBiz',$params);
    $business = $response->getBody();
    return View("myviewbiz")->with('business',json_decode($business));

Upvotes: 1

shalonteoh
shalonteoh

Reputation: 2074

$client = new \GuzzleHttp\Client();
$response = $client->post(
    'url',
    [
        GuzzleHttp\RequestOptions::JSON => 
        ['key' => 'value']
    ],
    ['Content-Type' => 'application/json']
);

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

Upvotes: 4

Siddiqui
Siddiqui

Reputation: 113

After testing with cURL, I found that the URL had been 'moved' to https instead of http. Using just http, the call was going through in Postman but not with Guzzle. The only change I made was to make the URL:

https://mail.zoho.com/api/accounts/

The website lists it as just http and the request does go through with PostMan. I have made prior calls with just http in Guzzle from the same API and they went through. If someone could help me understand why this happened and why this specific call when using http works in PostMan and not in Guzzle, that'd be great.

Upvotes: 0

Kavan Pancholi
Kavan Pancholi

Reputation: 602

You need to create data and headers in the same array and pass as a second argument. Use like this.

$client = new Client();
$URI = 'http://mail.zoho.com/api/accounts/'.$AccountId.'/messages';
$params['headers'] = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
$params['form_params'] = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
$response = $client->post($URI, $params);
echo "DONE!";

Good Luck!

Upvotes: 5

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

Ty to use:

$response = $client->post($URI, $headers, ['json' => $body]);

instead of

$Nbody = json_encode($body);
$response = $client->post($URI, $headers, $Nbody);

Upvotes: 0

Related Questions