title: Where and how to add a Bearer Token to upload an image to ASP.NET?
body: <p>I've followed <a href="https://github.com/survivingwithandroid/Surviving-with-android" rel="nofollow noreferrer">this Github page</a> and I've encouter problems with Bearer Token Authentication/Authorization in <a href="https://github.com/survivingwithandroid/Surviving-with-android/blob/master/HttpTestClient/src/com/survivingwithandroid/HttpClient.java" rel="nofollow noreferrer">this class</a>. <a href="https://github.com/survivingwithandroid/Surviving-with-android/blob/master/HttpTestClient/src/com/survivingwithandroid/UploadActivity.java" rel="nofollow noreferrer">In this class</a> the file (photo) is being uploaded.</p>
  
  <p>Overhere I've send a POST request to the server (running in Docker), which is deliverd. The server returns a 4XX error, because the authentication isn't working (yet).</p>
  
  <p>See here (POST with phone in Android):
  <a href="https://i.stack.imgur.com/UvN1K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UvN1K.png" alt="enter image description here"></a></p>
  
  <p>In this part of code I try to add the Bearer Token:</p>
  
  <pre><code> public void connectForMultipart() throws Exception {
      con = (HttpURLConnection) ( new URL(url)).openConnection();
      con.setRequestProperty("Authorization", "Bearer hereIsMyBearerToken.SomeTextAnd.SomeRandomNumbers132");
      con.setRequestMethod("POST");
      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      con.setRequestProperty("Connection", "Keep-Alive");
      con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
      con.setUseCaches(false);
      con.setDoInput(true);
      con.setDoOutput(true);
      con.connect();
      os = con.getOutputStream();
  }
  </code></pre>
  
  <p>Following to multiple sources the part <code>con.setRequestProperty("Authorization", "Bearer TOKENEHERE")</code> should work.</p>
  
  <p>In Postman it's working fine, so my token is right.
  See here (POST with Postman to test):
  <a href="https://i.stack.imgur.com/40nax.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/40nax.png" alt="enter image description here"></a></p>
  
  <p>Docker also sends 201 (OK) back:
  <a href="https://i.stack.imgur.com/gzt6Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gzt6Z.png" alt="enter image description here"></a></p>
  
  <p>I've also tried to authorized in the <code>UploadActivity</code> on the Github linked above. Without success.
  See here what I've tried in the <code>SendHttpRequestTask</code> class in the <code>UploadActivity</code>:</p>
  
  <pre><code>private class SendHttpRequestTask extends AsyncTask&lt;String, Void, String&gt; {
  
      @Override
      protected String doInBackground(String... params) {
          String url = params[0];
          //String param1 = params[1];
          //String param2 = params[2];
          Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.c10);
  
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          b.compress(Bitmap.CompressFormat.PNG, 0, baos);
  
          try {
              HttpClient client = new HttpClient(url);
              client.connectForMultipart();
              //client.addFormPart("param1", param1);
              //client.addFormPart("param2", param2);
              client.addFilePart("photo", "logo.png", baos.toByteArray());
              client.addFormPart("Bearer hereIsMyBearerToken.SomeTextAnd.SomeRandomNumbers132");
              client.finishMultipart();
              String data = client.getResponse();
          }
          catch(Throwable t) {
              t.printStackTrace();
          }
  
          return null;
      }
  </code></pre>
  
  <p>So where and how should I authorize my POST request?</p>