<p>It is pretty obvious now, but indeed the API is looking for json formatted values and I was in fact sending name:value pairs.</p>

<p>Here is what the updated logic looks like for anyone who comes across this.</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>$client = new \Guzzle\Http\Client('http://api.site.dev/');
$uri = 'my/endpoint/path';
$post_data = array(
    'craft_id' =&gt; $event-&gt;params['user']-&gt;id,
    'name' =&gt; $event-&gt;params['user']-&gt;friendlyName,
    'email' =&gt; $event-&gt;params['user']-&gt;email
);

$data = json_encode($post_data);

$request = $client-&gt;post($uri, array(
    'content-type' =&gt; 'application/json'
));

$request-&gt;setBody($data);
$response = $request-&gt;send();
</code></pre>

CC BY-SA 3.0
2016-08-15T21:25:16.533Z
2016-08-15T21:25:16.533Z
Damon
<p>I have a plugin that listens for new users to be created. I am then sending that user to system "B" via an API call. </p>

<p>All is working fine the old fashion way, I am curious how to accomplish the same using Guzzle.</p>

<pre><code>$client = new \Guzzle\Http\Client('http://api.site.dev/');
$uri = 'my/endpoint/path';

$request = $client-&gt;post($uri, array(
   'headers' =&gt; array('Content-type' =&gt; 'application/x-www-form-urlencoded'),
   'body' =&gt; array(
       'craft_id' =&gt; $event-&gt;params['user']-&gt;id,
       'name' =&gt; $event-&gt;params['user']-&gt;friendlyName,
       'email' =&gt; $event-&gt;params['user']-&gt;email
    ),
    'timeout' =&gt; 10
));

$response = $request-&gt;send();
</code></pre>

<p>Each time I get this response/error back:</p>

<pre><code>Client error response
[status code] 422
[reason phrase] Unprocessable Entity
</code></pre>

<p>Which tells me that system "B" isn't getting the information in the correct format. The docs aren't so great with post examples, so I'm not quite sure where I'm going down the wrong path.</p>

<p>Thank you for any suggestions!</p>

CC BY-SA 3.0
2016-08-15T20:54:20.623Z
Are the expecting the content to be JSON/XML encoded by chance?
Brad Bell
CC BY-SA 3.0
2016-08-15T21:21:17.090Z
Arg. Yep...that was the problem. Thanks Brad.
Damon
CC BY-SA 3.0
2016-08-15T21:21:47.147Z
NP! Be sure and add what worked as an answer, please. :)
Brad Bell
<plugin-development><guzzle>
How do I use Guzzle to send a post request from my plugin?
# How do I use Guzzle to send a post request from my plugin?

(Asked by: username_0 on 2016-08-15)

I have a plugin that listens for new users to be created. I am then sending that user to system "B" via an API call. 
All is working fine the old fashion way, I am curious how to accomplish the same using Guzzle.
$client = new \Guzzle\Http\Client('http://api.site.dev/');
$uri = 'my/endpoint/path';

$request = $client->post($uri, array(
   'headers' => array('Content-type' => 'application/x-www-form-urlencoded'),
   'body' => array(
       'craft_id' => $event->params['user']->id,
       'name' => $event->params['user']->friendlyName,
       'email' => $event->params['user']->email
    ),
    'timeout' => 10
));

$response = $request->send();

Each time I get this response/error back:
Client error response
[status code] 422
[reason phrase] Unprocessable Entity

Which tells me that system "B" isn't getting the information in the correct format. The docs aren't so great with post examples, so I'm not quite sure where I'm going down the wrong path.
Thank you for any suggestions!

**Tags:** plugin-development, guzzle
> **Comments:**
> **username_1**: Are the expecting the content to be JSON/XML encoded by chance?  
> **username_0**: Arg. Yep...that was the problem. Thanks Brad.  
> **username_1**: NP! Be sure and add what worked as an answer, please. :)  

---

## 1 answers

### Answer by username_0 on 2016-08-15. Score: 6 | Accepted answer

It is pretty obvious now, but indeed the API is looking for json formatted values and I was in fact sending name:value pairs.
Here is what the updated logic looks like for anyone who comes across this.
MyPlugin.php
$client = new \Guzzle\Http\Client('http://api.site.dev/');
$uri = 'my/endpoint/path';
$post_data = array(
    'craft_id' => $event->params['user']->id,
    'name' => $event->params['user']->friendlyName,
    'email' => $event->params['user']->email
);

$data = json_encode($post_data);

$request = $client->post($uri, array(
    'content-type' => 'application/json'
));

$request->setBody($data);
$response = $request->send();


---

