0

I'm using Buzz HTTP Client for Laravel.

I have a problem adding form data to my POST requests, since it wasn't specified in it's wiki/documentation.

Listed below are the two ways of sending requests.

Example 1:

$response = Buzz::post('http://api.website.com/login');
//how do I add a "username", and "password" field in my POST request?
echo $response;
echo $response->getContent;

Example 2:

$request = new Buzz\Message\Request('POST', '/', 'http://google.com');
$response = new Buzz\Message\Response();
//how do I add a "username", and "password" field in my POST request?    
$client = new Buzz\Client\FileGetContents();  
$client->send($request, $response);

echo $request;  
echo $response;
olleh
  • 1,248
  • 5
  • 16
  • 43

2 Answers2

3

The answer here is going to really depend on what the API expects. Lets assume, the API expects the password and username sent as JSON in the content of the request. The example http request would look something like:

POST /login HTTP/1.1
Content-Type: application/json

{
    "username": "bugsBunny",
    "password": "wh4tsUpD0c"
}

To do this with Buzz, this should work:

$jsonPayload = json_encode([
    ‘username’ => ‘bugsBunny’,
    ‘password’ => ‘wh4tsUpD0c
]);

$headers = ['Content-Type', 'application/json'];

$response = Buzz::post('http://api.website.com/login', $headers, $jsonPayload);

If you're attempting to submit a form on a given website, you shouldn't use the above method. Instead use Buzz's built in form method which will attach the correct headers.

use Buzz\Message\Form;
$request = new Form(Form::METHOD_POST, ‘login’, ‘api.website.com’);
$request->setFields([
    ‘username’ => ‘bugsBunny’,
    ‘password’ => ‘wh4tsUpD0c’
]);

$response = new Buzz\Message\Response();

$client = new Buzz\Client\Curl();
$client->send($request, $response);

On a side note, I'd suggest not using this library. The library is, as you stated, Laravel integration for Buzz. The issue here is, the author should have made buzz a dependency listed in composer, rather than include the Buzz source directly. This prevents updates to Buzz from making their way into this project. You can see on the actual Buzz repo, the last commit was 29 days ago. Also if another package is using Buzz and including it correctly by composer, composer would install both packages. But when an instance of Buzz was created, you couldn't be certain which version was being loaded. You should just use Buzz, which can be found on packagist.

// assuming $headers and $jsonPayload are the same as in previous example.
$browser = new Buzz\Browser();
$response = $browser->post('http://api.website.com/login', $headers, $jsonPayload);
Logan Bailey
  • 7,039
  • 7
  • 36
  • 44
  • hey just in time before I discovered the answer!! Never the less, thanks @LoganBailey for the help – olleh Oct 13 '14 at 07:33
  • Thank you for the definitive answer! Do you have any alternatives for this @LoganBailey? Yep I have also noticed the there haven't been commits for quite some time. Thanks for pointing out the issue regarding composer. – olleh Oct 13 '14 at 07:33
  • I'd suggest using the normal Buzz, with out Laravel integration. The added benefit of the Laravel integration is that it comes with a Facade class. But you don't really need this. I'll edit my answer to include the Buzz code. – Logan Bailey Oct 13 '14 at 07:36
  • 1
    Shouldn't the headers be set as $headers = ['Content-Type'=>'application/json']; instead of $headers = ['Content-Type', 'application/json']; in php? – Andrew Liu Sep 14 '16 at 01:23
0

It was foolish of me to not read the code first before asking.

The form data is actually pased on the third parameter for the function. Though it accepts strings only so don't forget to json encode your data.

Buzz Class

public function post($url, $headers = array(), $content = '')
{
....
....
}

Buzz::post($url, array(), json_encode(array('Username'=>'usernamexx','Password'=>'p@$$w0rD')) );
olleh
  • 1,248
  • 5
  • 16
  • 43
  • How can I send a file using this? Check my question at http://stackoverflow.com/questions/30783885/how-to-send-file-data-as-post-parameter-in-buzzbrowser-post-call – Vivek Vardhan Jun 12 '15 at 11:04