paula.em.lafon
paula.em.lafon

Reputation: 591

Accept request headers are "text/html,application/xhtm ...(etc)" when I want "application/json" in zf2 client code

I've got a fully working API set up, and the zf2 client I have set up works in theory, but no matter what, I can't seem to send a JSON request from my client to the API. Whenever I test it I keep getting the following headers which in turn return a 406 error:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Host: zf2-client.local
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 FirePHP/0.7.4
x-insight: activate

Now here's the class I'm using for this and the following is my request setup method. I have done some trials to get the headers in there, but they just don't work and i can't figure out why. As I said the API I'm consuming usually returns a perfectly fine json script, but right now it seems the headers are making it impossible for me to get the necesary information.

protected static function doRequest($url, array $postData = null, $method = Request::METHOD_GET)
{
    $client = self::getClientInstance();
    $client->setUri($url);
  /*
 Third attempt Also Didn't work

 $client
     ->setHeaders([
     'Content-Type' => 'application/json',
     ])
     ->setOptions(['sslverifypeer' => false])
     ->setMethod('POST')
     ->setRawBody(Json::encode($params));
   */
  $client->setMethod($method);


  /* 
 seccond attempt still not working

 $client->setHeaders(array(
 Zend\Http\Header\Accept::fromString('Accept: application/json'),
 'Accept-Encoding' => 'json',
 'X-Powered-By: Zend Framework',
 ));
  */

  //first attempt at adding a working application/json header      
  // $acceptHeader = Zend\Http\Header\Accept::fromString('Accept: application/json');
  //$client->getHeaders()->addHeader($acceptHeader);

    if ($postData !== null) {
        $client->setParameterPost($postData);
    }

    $response = $client->send();

    if ($response->isSuccess()) {
        return JsonDecoder::decode($response->getBody(), Json::TYPE_ARRAY);
    } else {
        $logger = new Logger;
        $logger->addWriter(new Stream('data/logs/apiclient.log'));
        $logger->debug($response->getBody());
        return FALSE;
    }
}

Any and all help is welcome. Thank you for your time and effort :)

Upvotes: 1

Views: 9821

Answers (1)

Wilt
Wilt

Reputation: 44316

The 406 response code means you have made a request with an Accept header that the server cannot satisfy.

Since you are consuming a Rest API that delivers json response you most likely need to send an Accept header that contains the application/json media type:

Accept: application/json

Or add it to the list of media types in your current accept header.

Accept: application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Since you did not provide any details on your client I cannot help you further. It is important to add all relevant details to your question to get a full answer. I can give an example in case you use a XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.setRequestHeader ("Accept", "application/json");

You can also test this in a tool like PostMan.

UPDATE:

I have noticed problems similar to yours with adding headers to a request in ZF2. What you could do is try once like this:

// get the headers from your client
$headers = $client->getHeaders();
// add the new accept header
$headers->addHeader($acceptHeader);
// set new headers object with the added accept header.
$client->setHeaders($headers);

I have to agree though with the comment on your question that it seems like this request you are processing is coming from a browser and not from your client object. If that is the case you should add the accept-header on the browser side.

Upvotes: 1

Related Questions