title: client.PostAsync hangs despite receiving a response and not incurring a deadlock
body: <p>I need to POST data to an AWS API Gateway, and I am using <code>client.PostAsync()</code> to do it. Here is the code for the method that makes the request:</p>
  <pre class="lang-cs prettyprint-override"><code>async Task&lt;string&gt; asyncAPI(string route, string password = &quot;&quot;, string data = &quot;&quot;, string service = &quot;&quot;)
  {
      HttpClient client = new HttpClient();
      Uri uri = new Uri(&quot;api&quot;);
      string json = JsonConvert.SerializeObject(new APIrequest(default, route, password, default, service, &quot;dawdasdqwd2&quot;));
      StringContent content = new StringContent(json, Encoding.UTF8, &quot;application/json&quot;);
      client.DefaultRequestHeaders.Add(&quot;x-api-key&quot;, &quot;key&quot;);
      HttpResponseMessage response = null;
      response = await client.PostAsync(uri, content);
      return await response.Content.ReadAsStringAsync();
  }
  </code></pre>
  <p>The code for the method that calls <code>asyncAPI()</code> is</p>
  <pre class="lang-cs prettyprint-override"><code>async Task processImage()
  {
      using (WebClient wc = new WebClient())
      {
          byte[] b = System.IO.File.ReadAllBytes(StringPhotoPath);
          string s = Convert.ToBase64String(b);
          string response=await asyncAPI(&quot;run_script&quot;, &quot;&quot;, s, &quot;Image_Processor&quot;);
          //more code here not relevant to the question
      }
  }  
  </code></pre>
  <p>Finally, here is the code that calls <code>processImage()</code>. It is also async, and is called when a button is pressed in the app (this project is Xamarin.Forms).</p>
  <pre class="lang-cs prettyprint-override"><code>private async void takePhoto(object sender, EventArgs e)
  {
      using(var progress = UserDialogs.Instance.Loading(&quot;Loading...&quot;))
      {
          await TakePhotoAsync();
          if (PhotoPath != null)
          {
              PhotoImage.Source = PhotoPath;
              if(PhotoGrid ==null)
              {
                  PhotoGrid = new AbsoluteLayout() { HeightRequest = PhotoImage.Height, WidthRequest = PhotoImage.Width };
                  PhotoMenu.Children.Add(PhotoGrid, Convert.ToInt32(PhotoImage.X), Convert.ToInt32(PhotoImage.Y));
              }
              else
              {
                  PhotoGrid.Children.Clear();
              }
              PhotoButton.IsVisible = false;
              PhotoMenu.IsVisible = true;
              await processImage();
          }
      }
  }
  </code></pre>
  <p>Since it's an async method calling an async method and no <code>.Result</code> or <code>ConfigureAwait(false)</code> is used I don't think it's possible that there is any deadlocking going on from blocking on async. However, when I breakpoint through asyncAPI the code hangs up and eventually times out on <code>client.PostAsync(...)</code></p>
  <p>I would think that this is a problem with the API, but I turned on CloudWatch logging and found that the API was working just fine. Here is the log for one of the runs:</p>
  <pre><code>(70c0f6d0-d89f-4714-9fc2-d44979c886b7) Extended Request Id: EOaW6FP2iYcF8Ww=
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Verifying Usage Plan for request: 70c0f6d0-d89f-4714-9fc2-d44979c886b7. API Key: **********************************TLcZh0 API Stage: uidvxrcio9/default
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Usage Plan check succeeded for API Key **********************************TLcZh0 and API Stage uidvxrcio9/default
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Starting execution for request: 70c0f6d0-d89f-4714-9fc2-d44979c886b7
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) HTTP Method: POST, Resource Path: /Blueline_General_API
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) API Key: **********************************TLcZh0
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) API Key ID: 5evbipy70k
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method request path: {}
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method request query string: {}
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method request headers: {x-api-key=**********************************TLcZh0, X-Forwarded-Proto=https, X-Forwarded-For=172.58.174.185, Host=uidvxrcio9.execute-api.us-east-2.amazonaws.com, X-Forwarded-Port=443, X-Amzn-Trace-Id=Root=1-611c0a92-3f8e5be1229e0ccb2d549848, Content-Type=application/json}
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method request body before transformations: {
      &quot;command&quot;: null,
      &quot;route&quot;: &quot;password_confirm&quot;,
      &quot;password&quot;: &quot;chiefwindfractionlie&quot;,
      &quot;sqlctype&quot;: null,
      &quot;service&quot;: &quot;&quot;,
      &quot;data&quot;: &quot;&quot;
  }
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Endpoint request URI: https://lambda.us-east-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-2:821930216915:function:Blueline_General_API/invocations
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Endpoint request headers: {x-amzn-lambda-integration-tag=70c0f6d0-d89f-4714-9fc2-d44979c886b7, Authorization=************************************************************************************************************************************************************************************************************************************************************************************************************************601604, X-Amz-Date=20210817T191426Z, x-amzn-apigateway-api-id=uidvxrcio9, X-Amz-Source-Arn=arn:aws:execute-api:us-east-2:821930216915:uidvxrcio9/default/ANY/Blueline_General_API, Accept=application/json, User-Agent=AmazonAPIGateway_uidvxrcio9, X-Amz-Security-Token=IQoJb3JpZ2luX2VjEFIaCXVzLWVhc3QtMiJIMEYCIQCv7g3zqjGQXaVMoAAuvAFuzwA5T4KClHwsCZ9B7sjmOgIhALAdux0OPaSQA5Uzki/BH50sOlNiOyh20Toe+6in4qcnKvoDCHsQAhoMNzE4NzcwNDUzMTk1IgyDrpWGGaECTHAmm/oq1wOkgn4rG/303C6njgV3/40y8A6paYVJoV/GFcKQCnKnhR/clpiVIfVmcI0WxUANd1lrOQ/jNuSVyA9yWgjv7Lk+VQRJ1Ae4cw+MmK3Ps7cPBMA6MPB/V0pFCDUFI+fMnwWKqukdzXY/HR+CFLSDdmUQpoW546 [TRUNCATED]
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Endpoint request body after transformations: {&quot;resource&quot;:&quot;/Blueline_General_API&quot;,&quot;path&quot;:&quot;/Blueline_General_API&quot;,&quot;httpMethod&quot;:&quot;POST&quot;,&quot;headers&quot;:{&quot;Content-Type&quot;:&quot;application/json&quot;,&quot;Host&quot;:&quot;uidvxrcio9.execute-api.us-east-2.amazonaws.com&quot;,&quot;X-Amzn-Trace-Id&quot;:&quot;Root=1-611c0a92-3f8e5be1229e0ccb2d549848&quot;,&quot;x-api-key&quot;:&quot;slq0NuO8XaubhaMS8QY98akA5h643F7rTRTLcZh0&quot;,&quot;X-Forwarded-For&quot;:&quot;172.58.174.185&quot;,&quot;X-Forwarded-Port&quot;:&quot;443&quot;,&quot;X-Forwarded-Proto&quot;:&quot;https&quot;},&quot;multiValueHeaders&quot;:{&quot;Content-Type&quot;:[&quot;application/json&quot;],&quot;Host&quot;:[&quot;uidvxrcio9.execute-api.us-east-2.amazonaws.com&quot;],&quot;X-Amzn-Trace-Id&quot;:[&quot;Root=1-611c0a92-3f8e5be1229e0ccb2d549848&quot;],&quot;x-api-key&quot;:[&quot;slq0NuO8XaubhaMS8QY98akA5h643F7rTRTLcZh0&quot;],&quot;X-Forwarded-For&quot;:[&quot;172.58.174.185&quot;],&quot;X-Forwarded-Port&quot;:[&quot;443&quot;],&quot;X-Forwarded-Proto&quot;:[&quot;https&quot;]},&quot;queryStringParameters&quot;:null,&quot;multiValueQueryStringParameters&quot;:null,&quot;pathParameters&quot;:null,&quot;stageVariables&quot;:null,&quot;requestContext&quot;:{&quot;resourceId&quot;:&quot;vnime3&quot;,&quot;resourcePath&quot;:&quot;/Blueline_General_API&quot;,&quot;httpMethod&quot;:&quot;POST&quot;,&quot;extendedRequestId&quot;:&quot;EOaW6FP2iYcF8Ww=&quot;,&quot;reque [TRUNCATED]
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Sending request to https://lambda.us-east-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-2:821930216915:function:Blueline_General_API/invocations
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Received response. Status: 200, Integration latency: 827 ms
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Endpoint response headers: {Date=Tue, 17 Aug 2021 19:14:27 GMT, Content-Type=application/json, Content-Length=136, Connection=keep-alive, x-amzn-RequestId=4052aff4-fbe6-4e48-b693-d8f41280c722, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-611c0a92-3f8e5be1229e0ccb2d549848;parent=7ff28da0447ec7fe;sampled=1}
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Endpoint response body before transformations: {
      &quot;isBase64Encoded&quot;: false,
      &quot;statusCode&quot;: 200,
      &quot;headers&quot;: {
          &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;
      },
      &quot;multiValueHeaders&quot;: {},
      &quot;body&quot;: &quot;\&quot;T\&quot;&quot;
  }
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method response body after transformations: &quot;T&quot;
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method response headers: {Access-Control-Allow-Origin=*, X-Amzn-Trace-Id=Root=1-611c0a92-3f8e5be1229e0ccb2d549848}
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Successfully completed execution
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) Method completed with status: 200
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) AWS Integration Endpoint RequestId : 4052aff4-fbe6-4e48-b693-d8f41280c722
  (70c0f6d0-d89f-4714-9fc2-d44979c886b7) X-ray Tracing ID : Root=1-611c0a92-3f8e5be1229e0ccb2d549848
  </code></pre>
  <p>Which is exactly what I want; the response returned by the Lambda function is correct.</p>
  <p><strong>Why might, despite proper async code and a correct API response, client.PostAsync(...) hang, and how can I fix it?</strong></p>