title: NodeJS : Validating request type (checking for JSON or HTML)
body: <p>I would like to check if the type that is requested by my client is JSON or HTML, as I want my route to satisfy both human and machine needs.</p>
  <p>I have read the Express 3 documentation at:</p>
  <blockquote>
  <p><a href="http://expressjs.com/api.html" rel="noreferrer">http://expressjs.com/api.html</a></p>
  </blockquote>
  <p>And there are two methods <code>req.accepts()</code> and <code>req.is()</code>, used like this:</p>
  <pre><code>req.accepts('json') 
  </code></pre>
  <p>or</p>
  <pre><code>req.accepts('html') 
  </code></pre>
  <p>Since these are not working as they should, I tried using:</p>
  <pre><code>var requestType = req.get('content-type');
  </code></pre>
  <p>or</p>
  <pre><code>var requestType = req.get('Content-Type');
  </code></pre>
  <p><code>requestType</code> is always <code>undefined</code>...</p>
  <p>Using the suggestion at this thread:</p>
  <blockquote>
  <p><a href="https://stackoverflow.com/questions/11727836/express-js-checking-request-type-with-is-doesnt-work">Express.js checking request type with .is() doesn&#39;t work</a></p>
  </blockquote>
  <p>does not work either. what am I doing wrong?</p>
  <hr />
  <p><strong>Edit 1</strong>: I have checked for correct client HTML negotiation. Here  are my two different request headers (taken from the debugger inspector):</p>
  <blockquote>
  <p>HTML : <code>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</code></p>
  <p>JSON : <code>Accept: application/json, text/javascript, */*; q=0.01</code></p>
  </blockquote>
  <hr />
  <p><strong>Solution</strong> (thanks to Bret):</p>
  <p>Turns out I was specifying the Accept headers wrong, and the <code>*/*</code> was the issue. Here is the code that works!</p>
  <pre><code>//server (now works)
  var acceptsHTML = req.accepts('html');
  var acceptsJSON = req.accepts('json');
  
  if(acceptsHTML)  //will be null if the client does not accept html
  {}
  </code></pre>
  <p>I am using JSTREE, a jQuery plugin that uses jQuery Ajax calls underneath). The parameters passed to the Ajax call are in the &quot;ajax&quot; field, and I have replaced the &quot;accepts&quot; parameter with a complete &quot;headers&quot; object. Now it works, and should solve the problem when you are using plain jQuery if it should ever occur.</p>
  <pre><code>//client 
  
  .jstree({
              // List of active plugins
              &quot;plugins&quot; : [
                  &quot;themes&quot;,&quot;json_data&quot;,&quot;ui&quot;,&quot;crrm&quot;,&quot;cookies&quot;,&quot;dnd&quot;,&quot;search&quot;,&quot;types&quot;,&quot;hotkeys&quot;,&quot;contextmenu&quot;
              ],
  
              &quot;json_data&quot; : {
                  &quot;ajax&quot; : {
                      // the URL to fetch the data
                      &quot;url&quot; : function(n) {
                          var url = n.attr ? IDMapper.convertIdToPath(n.attr(&quot;id&quot;)) : &quot;&lt;%= locals.request.protocol + &quot;://&quot; + locals.request.get('host') + locals.request.url %&gt;&quot;;
                          return url;
                      },
                      headers : {
                          Accept : &quot;application/json; charset=utf-8&quot;,
                          &quot;Content-Type&quot;: &quot;application/json; charset=utf-8&quot;
                      }
                  }
              }
          })
  </code></pre>