How to add document using cookie in Guzzle HTTP client-php


#1

Hi, I created a sync gateway document with sync gateway admin(http://localhost:4985). I want to create a doc with http://localhost:4984. I created a doc by cURL. but i am getting error when i create doc via Guzzle HTTP client.
I am using sync gateway 1.5.1, couchbase server 5.0, php 7.1.9 and Laravel 5.5
Error :

error : unauthorized, reason: login required

this is my code

$document_id ='test_001';
$data=['item_name'=>$params['itemName']];
$cookieJar = CookieJar::fromArray(['SyncGatewaySession' => $params['sync-session']],'localhost');
$client = new \GuzzleHttp\Client (['cookie' => $cookieJar,'allow_redirects' => false,'debug' => false]);
$response = $client->put("http://localhost:4984/pos/".$document_id,['body'=>json_encode($data)]); 

cURL

curl --cookie "SyncGatewaySession=97dcb859575a34690528303e3677a663cfab4879" -X POST "http://localhost:4984/pos/" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"item_name\": \"test_item\"}"

can anyone correct my code?
thank you.


#2

Hi,

It looks like the cookie isn’t being set properly in the PHP code. Looking at the GuzzleHttp docs, the ‘cookie’ field should actually be ‘cookies’. Please let us know if this doesn’t work for you.

http://docs.guzzlephp.org/en/stable/quickstart.html#cookies

Thanks,
Ben


#3

Hi bbrks,
I corrected like this. now it is working fine.
$jar = new \GuzzleHttp\Cookie\CookieJar;
$cookieJar = $jar->fromArray($values,$domain);

thank you.