Index: /tools/gformat.py
===================================================================
--- /tools/gformat.py	(revision 13935)
+++ /tools/gformat.py	(revision 13936)
@@ -1748,8 +1748,12 @@
 def is_text_request(environ=os.environ):
   """ Find out whether we are calling from the CLI or any text based CLI utility """
-  try:
-    return environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
-  except KeyError:
+  if 'CONTENT_TYPE' in os.environ and os.environ['CONTENT_TYPE'] == 'text/plain':
     return True
+
+  if 'HTTP_USER_AGENT' in environ:
+    return any([os.environ['HTTP_USER_AGENT'].lower().startswith(x) for x in ['curl', 'fetch', 'wget']])
+  else:
+    return False
+
 
 def switchFormat(setting):
