2

i wrote a .json file and want to download it with AFNetworking. But AFNetworking complains: fail Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/plain

my JSON file test.json

{
"count-packages": 5,
"packages": 
{
    "de":
    {
        "0": "Wackel Dackel",
        "1": "Hans Wurst",
        "2": "Peter Ploes",
        "3": "Tiffel Toffel",
        "4": "China Mann"
    },
    "en":
    {
        "0": "Wobble dachshund",
        "1": "Hans Sausage",
        "2": "Peter Ploes",
        "3": "Tiffel Potato",
        "4": "Peking Ente"
    }
}

}

HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 17:31:06 GMT
Server: Apache/1.3.41 Ben-SSL/1.59
Last-Modified: Mon, 06 Feb 2012 17:28:13 GMT
ETag: "18039c71-205-4f300dad"
Accept-Ranges: bytes
Content-Length: 517
Content-Type: text/plain

How can I change the Content-Type?

zeiteisen
  • 6,877
  • 5
  • 44
  • 68
  • I found a bug in the AFNetworking class AFHTTPRequestOperation method -(BOOL)hasAcceptableContentType - (BOOL)hasAcceptableContentType { return !self.acceptableContentTypes || [self.acceptableContentTypes containsObject:[self.response MIMEType]]; to - (BOOL)hasAcceptableContentType { NSLog(@"mime type %@", [self.response MIMEType]); return !self.acceptableContentTypes || [self.acceptableContentTypes member:[self.response MIMEType]]; } – zeiteisen Feb 09 '12 at 15:40

4 Answers4

3

It is the issue on the server side. If you have control to the Apache server, add following line in httpd.conf

application/json            json

EDIT : I was wrong of the syntax with my answer above. The lines like above should go into mime.types file as @James suggests. But you can use AddType directive and specify mime types in httpd.conf as well.

AddType application/json .json
thomaux
  • 17,387
  • 9
  • 71
  • 94
barley
  • 4,297
  • 22
  • 28
  • 1
    Wrong file. It's mime.types that contains these mappings. – JamieSee Feb 06 '12 at 17:58
  • Thanks @James, I stand corrected. But it is also possible to specify in httpd.conf with slightly different syntax. I edit my answer accordingly. – barley Feb 06 '12 at 18:07
  • You shouldn't change the mime.types files because if you upgrade the server this file will be overwritten as said in [Apache docs](http://httpd.apache.org/docs/current/mod/mod_mime.html#typesconfig) so the **EDIT** is the correct answer. – J. Costa Aug 31 '12 at 15:47
1

If you don't have access to httpd.conf (like in many cases) you can create a .htaccess file and simply write AddType application/json .json inside. Put it next your json file and it should be OK !

adriendenat
  • 3,310
  • 1
  • 23
  • 25
1

You'll need to add an appropriate entry for .json to the Apache mime.types file. See Setting up MIME Types with Apache

JamieSee
  • 11,832
  • 2
  • 28
  • 44
-1

You can't change the content type for a .json file. Probably the error comes from the javascript, ther you sould change or parse as Json

Tolo Palmer
  • 1,714
  • 2
  • 17
  • 27