Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PestJSON forces Accept and Content-Type headers #63

Open
Gamesh opened this issue Jan 27, 2015 · 2 comments
Open

PestJSON forces Accept and Content-Type headers #63

Gamesh opened this issue Jan 27, 2015 · 2 comments

Comments

@Gamesh
Copy link

Gamesh commented Jan 27, 2015

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
Copy link

tm1000 commented Feb 2, 2015

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

@Gamesh
Copy link
Author

Gamesh commented Feb 2, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants