title: How to store result of exchange.getRequestURI().getQuery() in variable and use it to get authorization code
body: <p>I'm trying to implement this stage of project - <a href="https://hyperskill.org/projects/62/stages/337/implement" rel="nofollow noreferrer">https://hyperskill.org/projects/62/stages/337/implement</a>.
  I need to store result of <code>String code = exchange.getRequestURI().getQuery();</code> in a variable and use it in POST request.</p>
  
  <p><code>POST(HttpRequest.BodyPublishers.ofString("client_id=" + CLIENT_ID + "&amp;client_secret=" + CLIENT_SECRET+ "&amp;grant_type=" + GRANT_TYPE + "&amp;code=" + CODE + "&amp;redirect_uri=" + REDIRECT_URI))</code></p>
  
  <p>But I can't do that, because of it doesn't store in a vairable outside of <strong>server.createContext</strong>.
  Maybe I'm trying to do wrong thing? Can anyone help me?</p>
  
  <pre><code>
      private static final String CLIENT_ID = "da072c60fcee469e8b0f4140aa4480d5";
      private static final String CLIENT_SECRET = "8ada13093c704487b57c3a660448884e";
      private static final String AUTHORIZE_ADDRESS = "https://accounts.spotify.com/authorize";
      private static final String RESPONSE_TYPE = "code";
      private static final String TOKEN_ADDRESS = "https://accounts.spotify.com/api/token";
      private static final String GRANT_TYPE = "authorization_code";
      private static final String CODE = "";
      private static final String REDIRECT_URI = "http://localhost:8080";
      private static final String ANSWER_DENIED_ACCESS = "Please, provide access for application.";
  
      public static void main(String[] args) throws Exception {
          Scanner sc = new Scanner(System.in);
          boolean successfulAccess = false;
  
          while (sc.hasNext()) {
              String input = sc.next();
              switch (input) {
                  case "auth":
                      server();
                      request();
                      successfulAccess = true;
                      System.out.println("---SUCCESS---");
                      break;
              }
          }
      }
  
      private static void server() throws IOException {
          HttpServer server = HttpServer.create();
          server.bind(new InetSocketAddress(8080), 0);
  
          server.start();
  
          System.out.println("use this link to request the access code:");
          System.out.println(AUTHORIZE_ADDRESS
                  + "?client_id=" + CLIENT_ID
                  + "&amp;redirect_uri=" + REDIRECT_URI
                  + "&amp;response_type=" + RESPONSE_TYPE);
          System.out.println("waiting for code...");
  
          server.createContext("/",
                  exchange -&gt; {
                      String code = exchange.getRequestURI().getQuery();
                      String result = "";
                      String answer = "";
  
                      if (code.contains("code")) {
                          result = "Got the code. Return back to your program.";
                          answer = "code received";
                      } else {
                          result = "Not found authorization code. Try again.";
                          answer = "code didn't received";
                      }
  
                      exchange.sendResponseHeaders(200, result.length());
                      exchange.getResponseBody().write(result.getBytes());
                      exchange.getResponseBody().close();
  
                      System.out.println(answer);
                  }
          );
  
          server.stop(10);
      }
  
      private static void request() throws IOException, InterruptedException {
  
          System.out.println("making http request for access_token...");
  
          HttpRequest request = HttpRequest.newBuilder()
                  .POST(HttpRequest.BodyPublishers.ofString(
                          "client_id=" + CLIENT_ID
                                  + "&amp;client_secret=" + CLIENT_SECRET
                                  + "&amp;grant_type=" + GRANT_TYPE
                                  + "&amp;code=" + CODE
                                  + "&amp;redirect_uri=" + REDIRECT_URI))
                  .header("Content-Type", "application/x-www-form-urlencoded")
                  .uri(URI.create(TOKEN_ADDRESS))
                  .build();
  
          HttpClient client = HttpClient.newBuilder().build();
          HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandlers.ofString());
  
          System.out.println("response:");
          System.out.println(response.body());
      }
  }```
  </code></pre>