android - When is it necessary to specific application/json Content-Type explicitly -


currently, i'm building android mobile app & python restful server services.

i found that, makes no different, whether or not i'm using

self.response.headers['content-type'] = "application/json" 

the following code (which doesn't specific content-type explicitly) works fine me. wondering, in situation, should specific content-type explicitly?


python restful server services code

class debughandler(webapp2.requesthandler):     def get(self):         response = {}         response["key"] = "value"         self.response.out.write(json.dumps(response))  application = webapp2.wsgiapplication([     ('/debug', debughandler), ], debug = true) 

android mobile app client code

public static string getresponsebodyasstring(string request) {     bufferedreader bufferedreader = null;     try {         url url = new url(request);         httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection();         inithttpurlconnection(httpurlconnection);         inputstream inputstream = httpurlconnection.getinputstream();         bufferedreader = new bufferedreader(new inputstreamreader(inputstream));          int charread = 0;         char[] buffer = new char[8*1024];         // use stringbuilder instead of stringbuffer. not concern         // on thread safety. stringbuffer = new stringbuffer();         stringbuilder stringbuilder = new stringbuilder();         while ((charread = bufferedreader.read(buffer)) > 0) {             stringbuilder.append(buffer, 0, charread);         }         return stringbuilder.tostring();     } catch (malformedurlexception e) {         log.e(tag, "", e);     } catch (ioexception e) {         log.e(tag, "", e);     } {         close(bufferedreader);     }     return null; } 

content-type specifies what's inside response (i.e. how interpret body of response). json, html document, jpeg, etc? useful when have different representations of resources , accept it's header involved in doing content negotiation between client , server.

different clients might need different formats. c# client might prefer xml, javascript client might prefer json, client work multiple representations try request efficient 1 first , settle others if server can't serve preferred one, etc.

content-type important in browser user agent knows how display response. if don't specify 1 browser try guess, based on extension , maybe fallback save as... dialog if fails also. in browser, lack of content-type might cause html open save as... dialog, or pdf file rendered gibberish in page.

in application client, not having content-type might cause parsing error or might ignored. if server serves json , client expects json can ignore content-type, client assume it's json because that's how built.

but if @ point want add xml representation, or yaml or whatever? have problem because client assumed it's json , ignored content-type. when receives xml try parse json , fail. if instead client built content types in mind , specify content-type client take account , select appropriate parser instead of blindly making assumptions.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -