Skip to content
This repository
Browse code

add force_json=true to force 'application/json' response type.

  • Loading branch information...
commit ea19e55420dd0201a0cbb3dddbe06fcd7ba90e08 1 parent b507ea2
authored
1  README.md
Source Rendered
@@ -282,6 +282,7 @@ You can perform all types of queries using Lucene's default <a href="http://luce
282 282
 <dt>analyzer</dt><dd>The analyzer used to convert the query string into a query object.
283 283
 <dt>callback</dt><dd>Specify a JSONP callback wrapper. The full JSON result will be prepended with this parameter and also placed with parentheses."
284 284
 <dt>debug</dt><dd>Setting this to true disables response caching (the query is executed every time) and indents the JSON response for readability.</dd>
  285
+<dt>force_json<dt><dd>Usually couchdb-lucene determines the Content-Type of its response based on the presence of the Accept header. If Accept contains "application/json", you get "application/json" in the response, otherwise you get "text/plain;charset=utf8". Some tools, like JSONView for FireFox, do not send the Accept header but do render "application/json" responses if received. Setting force_json=true forces all response to "application/json" regardless of the Accept header.</dd>
285 286
 <dt>include_docs</dt><dd>whether to include the source docs</dd>
286 287
 <dt>limit</dt><dd>the maximum number of results to return</dd>
287 288
 <dt>q</dt><dd>the query to run (e.g, subject:hello). If not specified, the default field is searched.</dd>
22  src/main/java/com/github/rnewson/couchdb/lucene/SearchRequest.java
@@ -75,7 +75,7 @@
75 75
 
76 76
     private final String ifNoneMatch;
77 77
 
78  
-	private final String contentType;
  78
+    private final String contentType;
79 79
 
80 80
     public SearchRequest(final JSONObject obj, final String viewsig) throws ParseException {
81 81
         final JSONObject headers = obj.getJSONObject("headers");
@@ -92,19 +92,19 @@ public SearchRequest(final JSONObject obj, final String viewsig) throws ParseExc
92 92
         this.include_docs = query.optBoolean("include_docs", false);
93 93
         this.rewrite_query = query.optBoolean("rewrite", false);
94 94
         this.callback = query.optString("callback", null);
95  
-        
  95
+
96 96
         // Negotiate Content-Type of response.
97  
-        if (headers.optString("Accept").indexOf("application/json") != -1) {
98  
-        	this.contentType = "application/json";
  97
+        if (query.optBoolean("force_json", false) || headers.optString("Accept").indexOf("application/json") != -1) {
  98
+            this.contentType = "application/json";
99 99
         } else {
100  
-        	this.contentType = "text/plain;charset=utf-8";
  100
+            this.contentType = "text/plain;charset=utf-8";
101 101
         }
102 102
 
103 103
         // Parse query.
104 104
         final Analyzer analyzer = AnalyzerCache.getAnalyzer(query.optString("analyzer", "standard"));
105 105
         final QueryParser parser = new QueryParser(Config.DEFAULT_FIELD, analyzer);
106 106
         if ("AND".equalsIgnoreCase(Config.DEFAULT_OPERATOR)) {
107  
-        	parser.setDefaultOperator(Operator.AND);
  107
+            parser.setDefaultOperator(Operator.AND);
108 108
         }
109 109
         this.q = parser.parse(query.getString("q"));
110 110
 
@@ -260,8 +260,8 @@ public String execute(final IndexSearcher searcher) throws IOException {
260 260
         result.put("code", 200);
261 261
 
262 262
         final JSONObject headers = new JSONObject();
263  
-		headers.put("Content-Type", contentType);
264  
-		// Allow short-term caching.
  263
+        headers.put("Content-Type", contentType);
  264
+        // Allow short-term caching.
265 265
         headers.put("Cache-Control", "max-age=" + Config.COMMIT_MAX / 1000);
266 266
         // Results can't change unless the IndexReader does.
267 267
         headers.put("ETag", etag);
@@ -270,9 +270,9 @@ public String execute(final IndexSearcher searcher) throws IOException {
270 270
             headers.put("Content-Type", "text/plain;charset=utf-8");
271 271
             result.put("body", escape(json.toString(2)));
272 272
         } else if (callback != null)
273  
-        	result.put("body", String.format("%s(%s)", callback, json));
274  
-        else	
275  
-        	result.put("json", json);        
  273
+            result.put("body", String.format("%s(%s)", callback, json));
  274
+        else
  275
+            result.put("json", json);
276 276
 
277 277
         // Include headers.
278 278
         result.put("headers", headers);

0 notes on commit ea19e55

Please sign in to comment.
Something went wrong with that request. Please try again.