title: How to send HTTP request using JWT token for authentication from cookie storage in android
body: <p><strong>What I did so far</strong>:</p>
  
  <p>I am trying to communicate with Java web application which has custom authentication. In that, I need to first hit a link with request body parameters <code>JSON</code> type to get <a href="https://auth0.com/docs/server-apis/java" rel="noreferrer">JWT</a> <code>auth-token</code> in my cookies.</p>
  
  <p>I have tested connection in <a href="https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop" rel="noreferrer">Postman</a>, I am receiving proper <code>JSON</code> response. But when I try same in my android application it return <code>Bad Request</code> error.</p>
  
  <p><strong>For Postman testing:</strong></p>
  
  <p>For login and getting <code>auth-token</code> in cookie storage:</p>
  
  <ul>
  <li>Post, URL: <code>http://iitjeeacademy.com/iitjeeacademy/api/v1/login</code></li>
  <li>Headers: <code>Content-Type:application/json</code></li>
  <li>Request body (raw): <code>{"password":"123","type":"student","email":"shobhit@gmail.com"}</code></li>
  </ul>
  
  <p>After login getting response using:</p>
  
  <ul>
  <li>Get, URL: <code>http://iitjeeacademy.com/iitjeeacademy/api/v1/student/me</code></li>
  </ul>
  
  <p><em>Screenshot of cookie stored in Postman:</em>
  <a href="https://i.stack.imgur.com/q2NTh.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/q2NTh.jpg" alt="Postman screenshot of stored cookie"></a></p>
  
  <p><em>Screenshot of cookie stored in Chrome</em>
  <a href="https://i.stack.imgur.com/VTsnN.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/VTsnN.jpg" alt="enter image description here"></a></p>
  
  <p>Following are my <code>HttpURLConnection</code> request codes in android:</p>
  
  <p><strong>"Post" method</strong>, this connection is used to get <code>auth-token</code>. <em>This method returns 200 Response.</em></p>
  
  <pre><code>HttpURLConnection connection = null;
  try {
          // Created URL for connection.
          URL url = new URL(link);
  
          // Input data setup
          byte[] postData = request.getBytes(StandardCharsets.UTF_8);
          int postDataLength = postData.length;
  
          // Created connection
          connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setInstanceFollowRedirects(false);
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("charset", "utf-8");
          connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
          connection.setUseCaches(false);
  
          // loaded inputs
          DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
          wr.write(postData);
          wr.flush();
          wr.close();
  
          // getting a response
          int responseCode = connection.getResponseCode();
          if (responseCode == HttpURLConnection.HTTP_OK){
              // Read response
              response = convertToString(connection.getInputStream());
              return response;
          }else{
              // Read Error
              String response = connection.getResponseMessage();
              return response;
          }
      } catch (MalformedURLException e) {
          e.printStackTrace();
          Log.v("MalformedURL ---&gt; ", e.getMessage());
      } catch (ProtocolException p) {
          p.printStackTrace();
          Log.v("Connection ---&gt; ", p.getMessage());
      } catch (IOException i) {
          i.printStackTrace();
          Log.v("IO Exception ---&gt; ", i.getMessage());
      } finally {
          connection.disconnect();
      }
  </code></pre>
  
  <p><strong>"Get" method</strong>, must have <code>auth-token</code> in session cookies to get response. <em>This method gives an 401 Unauthorized Error.</em></p>
  
  <pre><code>HttpURLConnection connection = null;
  try{
          // Created URL for connection
          URL url = new URL(link);
  
          // Created connection
          connection = (HttpURLConnection) url.openConnection();
          connection.setInstanceFollowRedirects(false);
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("charset", "utf-8");
  
          // getting a response
          int responseCode = connection.getResponseCode();
          if (responseCode == HttpURLConnection.HTTP_OK){
              response = convertToString(connection.getInputStream());
              return response;
          }else{
              // Read Error
              String response = connection.getResponseMessage();
              return response;
          }
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException p) {
          p.printStackTrace();
      } catch (IOException i) {
          i.printStackTrace();
      } finally {
          connection.disconnect();
      }
  </code></pre>
  
  <p><strong>Question:</strong>
  How to use stored JWT Token from cookies in <code>HttpURLConnection</code> android to get response from web service.</p>