educoder / pest

A proper REST client for PHP.
356 stars 123 forks source link

PestJSON forces Accept and Content-Type headers #63

Open Gamesh opened 9 years ago

Gamesh commented 9 years ago

Hi,

in PestJson there are harcoded headers

protected function prepRequest($opts, $url)
{
$opts[CURLOPT_HTTPHEADER][] = 'Accept: application/json';
$opts[CURLOPT_HTTPHEADER][] = 'Content-Type: application/json';
return parent::prepRequest($opts, $url);
}

the problem is that they cannot be changed and in some situations, like working with Microsoft Sharepoint API you need a little bit different format:

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose

maybe these default headers could be stored in a protected property, that can be overridden if needed, that would be more flexible.

tm1000 commented 9 years ago

You could just extend PestJSON and overwrite the prepRequest method. Thats the whole point of this library anyways, extensibility through extends/classes.

Gamesh commented 9 years ago

i'm well aware of that, but then you have to call parent's parent, that's really ugly and if you extend a class and replace entirely one of it's methods it is considered a bad practice.

if these headers would be moved to a protected property

protected $defaultHeaders = array(
'Accept: application/json',
'Content-Type: application/json',
);

protected function prepRequest($opts, $url)
{
  if (isset($opts[CURLOPT_HTTPHEADER])){
    $opts[CURLOPT_HTTPHEADER] = array_merge($this->defaultHeaders, $opts[CURLOPT_HTTPHEADER]);
  }
  return parent::prepRequest($opts, $url);
}

then you could just define your own property $defaultHeaders in extended class and change default headers without replacing an entire method.