0

currently the server returns the following headers like so:

2014-11-13 00:20:04.079 myiOSApplication 46715:1003]:

{ status code: 200, headers {
    Connection = close;
    "Content-Length" = 5;
    "Content-Type" = "text/html; charset=UTF-8";
    "Content-Typesss" = "application/json; charset=UTF-8"; //<-- Notice this appears, but soon as I remove the extra s characters, then it wont override the line above. :( so the content always returns `text/html` instead of the desired `application/json`
    Date = "Thu, 13 Nov 2014 00:20:03 GMT";
    Server = "Apache/2.2.26 (Amazon)";
    Status = "200 OK";
    StatusCode = 200;
    "X-Powered-By" = "PHP/5.3.28";
} }

I can set other headers with ease, but as SOON as I try to set a header with the key: Content-Type like so:

header('Content-Type : application/json; charset=UTF-8');

then my key gets overridden with the system one and ignores my header type.

What can I do?

Update 1:

In response to @scuzzy's request for -

What happens if you echo your header then exit(); the script? eg header('Content-Type : application/json; charset=UTF-8');exit('{"hello":"world"}');

Here's the iOS response to that:

2014-11-13 00:59:25.801 myiOSApplication [46849:1f07] RESPONSE: <NSHTTPURLResponse: 0x61800002c060> { URL: http://www.riabp.com/KINGS/Secure/Rajam/Get/Employees } { status code: 200, headers {
    Connection = close;
    "Content-Length" = 18;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 13 Nov 2014 00:59:25 GMT";
    Server = "Apache/2.2.26 (Amazon)";
    "X-Powered-By" = "PHP/5.3.28";
} }
2014-11-13 00:59:25.801 KingsEMS[46849:303] Error: Request failed: unacceptable content-type: text/html
2014-11-13 00:59:25.801 KingsEMS[46849:303] JSON Error:     {"hello":"world"}
Pavan
  • 17,840
  • 8
  • 59
  • 100

3 Answers3

1

I'm not sure if this is still an active question but I've had a similar issue whereby the content-type header was being set by PHP for text/html but when I wanted to download an image, using PHP, I couldn't update the header content-type to image/gif so the file downloaded but couldn't be opened.

The answer in the end was to return the output buffer and make sure it's empty:

$html = ob_get_clean();
$html = str_replace(" ", "", $html);

I am assuming that there was some white space somewhere in the code that was forcing the output to remain as the original content-type.

Richard

UPDATE: this should be added before the new header value is set, in case this wasn't obvious.

richard.holmes
  • 101
  • 1
  • 7
0

If you are using a framework like Silex or Zend etc.etc or just writing normal php code and you're using these functions

 json_encode($query)         //php json function
 $app->json($query)          //Silex framework function`

but none of these solves your need to receive a json content Type,there is a little trick that can help you :

     header('Content-Type: application/json');  //before everything
     $query =....                              //retrieve your data in every way you need
     $queryjsoned=json_encode($query);    //jsonify your query
     exit($json);                      //it's very important,it solves your life
     return $queryjsoned;             //if you are in a function/Route,but it isn't mandatory

it's very strange that you need to do an exit to override the content type.

Fedeco
  • 846
  • 10
  • 28
  • discovery 2 : Also if the query is wrong, and you encode it in a json, the json serialize function will give you an error and return plain text html. – Fedeco Oct 18 '16 at 12:56
0

Very old question, but one I recently faced. Just in case someone has this problem and the solution (as for me) is this simple, here it is: If you use PHP include files, e.g.

include ("config.php");

Make sure that you don't include a PHP end tag (?>) at the end, unless you need it. This will prevent "unwanted whitespace" as described in PHP end tag "?>". Such whitespace will trigger the default MIME type config in PHP.

In my case I did have an end tag and an empty line after it. Removing the extra lines solved the issue (declared content-type headers are now used).