I try to authenticate a user via a RESTful API in combination with CouchDB and Httpful.
The client sends the credentials of the user to my server and the server forwards them to the CouchDB server.
When I var_dump
the response, I can see that the content is there but I can't extract the cookie which I would like to send back to the client.
Authenticate function on my Server:
public function authenticate($request, $response)
{
$username = $request->getParsedBody()['username'];
$password = $request->getParsedBody()['password'];
$uri = "http://localhost:5984/_session";
$sessionToken = \Httpful\Request::post($uri)
->sendsJson()
->body('{"name":"'.$username.'", "password":"'.$password.'"}')
->send();
$cookie = $sessionToken->headers->headers[6];
return $response->withHeader('Set-Cookie', $cookie);
}
The response object:
object(Httpful \ Response) # 56(13) {
["body"] => object(stdClass) # 58(3)
...
["raw_body"]
...
["headers"] => object(Httpful \ Response \ Headers) # 57(1) {
["headers": "Httpful\Response\Headers": private] => array(6) {
...
["set-cookie"] => string(85)"AuthSession=abc123; Version=1; Path=/; HttpOnly"
}
}
["raw_headers"]
...
["code"]
...
}
Error message:
Cannot access private property Httpful\Response\Headers::$headers
I would like to know how I could access the Set-Cookie
attribute with Httpful. I browsed the web on this issue and unfortunately it seems that I'm the only one who is facing this particular problem.
I found a solution:
I just needed to add these two lines to my authentication controller:
$myArray = array_values($sessionToken->_parseHeaders($sessionToken->raw_headers));
$cookie = $myArray[5];
The _parseHeaders
I found within the API documentation from Httpful and played around with it in combination with array_values
.
An instance of Headers
-class is set into $sessionToken->headers
, that you can just access like an array.
The set-cookie
header you get with this:
$sessionToken->headers['set-cookie'];