You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In your current implementation, the method call getHeaders() only returns the header status code (ie. 400) but we may need to distinguish the response header using the corresponding phrase as well. It would also be nice to parse it into constituent parts. How can we get it ?
What's expected?
Additional granularity of the HTTP status line header returned from getHeaders(). For example, assume the raw response headers from a call were:
After some investigations, I was able to obtain my expected result by overriding createResponse and extending httpclient\Client. In the code below, you could remove the bit about filtering out '(request id=' as that is specific to my application. For example,
php
use yii\httpclient\Client as Client;
class modClient extends Client
{
/**
* {@inheritDoc}
*/
public function createResponse($content = null, array $headers = [])
{
return parent::createResponse( $content, $this->modifyHeaders($headers) );
}
/**
* Returns the modified headers.
* This function appends additional headers to $headers.
* The additional headers consist of:
* 'http-status-line': The full status line (ie. 'HTTP/1.1 200 OK')
* 'http-version': The http version returned from the server (ie. 'HTTP/1.1')
* 'http-status-code': The Status code (ie. '200')
* 'http-reason-phrase': The reason phrase (ie. 'OK')
* 'http-status-code-reason-phrase': The status line without the HTTP version (ie. '200 OK')
* Additional filtering removes everything after and including '(request id=' from 'http-reason-phrase' and 'http-status-line' if found.
* @param array $headers headers list.
* @return array modified headers list
*/
public function modifyHeaders(array $headers = [])
{
$newHeaders = $headers;
$addHeaders = [
'http-status-line' => '',
'http-version' => '',
'http-status-code' => '',
'http-reason-phrase' => '',
'http-status-code-reason-phrase' =>'',
];
if (!is_object($newHeaders)) {
if (is_array($newHeaders)) {
foreach ($newHeaders as $name => $value) {
if (is_int($name)) {
// parse raw header :
$rawHeader = $value;
if (strpos($rawHeader, 'HTTP/') === 0) {
//removes everything after and including '(request id='
if( ($separatorPos = strpos($rawHeader,'(request id=')) !== false ) {
$rawHeader = trim(substr($rawHeader, 0, $separatorPos));
}
//parse out the various parts
$parts = explode(' ', $rawHeader, 3);
$addHeaders['http-status-line'] = $rawHeader;
$addHeaders['http-version'] = $parts[0];
$addHeaders['http-status-code'] = sizeof($parts)>1?trim($parts[1]):'';
$addHeaders['http-reason-phrase'] = sizeof($parts)>2?trim($parts[2]):'';
$addHeaders['http-status-code-reason-phrase'] = $addHeaders['http-status-code']. ' '.$addHeaders['http-reason-phrase'];
}
}
}
}
}
return array_merge($addHeaders,$newHeaders);
}
}
With the override, and using the extended client the same original headers provides added granularity. For example,
Adds 3 new header keys to $headerCollection.
1. 'http-status-line': The full status line (ie. 'HTTP/1.1 200 OK')
2. 'http-version': The http version returned from the server (ie. 'HTTP/1.1')
3.'http-status-code-reason-phrase': The status line without the HTTP version (ie. '200 OK')
What steps will reproduce the problem?
Assume possible response headers of these forms.
HTTP:/1.1 400 InvalidParamSince
HTTP:/1.1 400 MissingParamSince
HTTP:/1.1 400 MissingQueryParams
HTTP:/1.1 400 NoMatchForQueryParams
In your current implementation, the method call getHeaders() only returns the header status code (ie. 400) but we may need to distinguish the response header using the corresponding phrase as well. It would also be nice to parse it into constituent parts. How can we get it ?
What's expected?
Additional granularity of the HTTP status line header returned from getHeaders(). For example, assume the raw response headers from a call were:
Expected output would provide added granularity. For example,
What do you get instead?
Additional info
After some investigations, I was able to obtain my expected result by overriding createResponse and extending httpclient\Client. In the code below, you could remove the bit about filtering out '(request id=' as that is specific to my application. For example,
With the override, and using the extended client the same original headers provides added granularity. For example,
The text was updated successfully, but these errors were encountered: