title: Sending a Post Request to register a new user to a Web API, but receiving 400 : BadRequest
body: <p>Having issues with my code <code>[ Throwing an Unhandled exception: System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request) ]</code> when trying to connect to WebApi</p>
  <p>This is my first time working with await/async methods, but I am needing to return
  <code>string msgTask = await response.Content.ReadAsStringAsync(); return msgTask;</code></p>
  <p>At first my Console.WriteLine(await response.Content.ReadAsStringAsync(); returned:
  <code>BadRequest {&quot;error&quot;:&quot;Password must be at least 8 characters, with at least 1 of each alpha, number and special characters&quot;}</code></p>
  <p>But then I inserted this check: <code>response.EnsureSuccessStatusCode();</code> which Throws the System.Net.Http.HttpRequestException</p>
  <p>Full [top-level styled] Code Below (<strong>I am only using Console.WriteLine() to help with debugging</strong>, final code will only have <code>return msgTask;</code>) :</p>
  <pre><code>
  HttpRequest.GetHttpResponse();
  await WattTime.PostRequest.RegisterUser();
  
  public class HttpRequest
  {
     public static HttpClient client = new();
     
      public static void GetHttpResponse()
      {
        // GetRequestMethod to use later
      }
  }
  namespace WattTime
  {
      class PostRequest : HttpRequest
      {
          public static async Task&lt;string&gt; RegisterUser()
          {
              string Url = &quot;https://api2.watttime.org/v2/register&quot;;
              Dictionary&lt;string, string&gt; parameters = new()
              {
                  {&quot;username&quot;, &quot;TestUser&quot; },
                  {&quot;password&quot;, &quot;Password@1&quot; },
                  {&quot;email&quot;, &quot;testuser@yahoo.com&quot; },
                  {&quot;org&quot;, &quot;XYZ&quot; },
              };
              var jsonDictionary = JsonConvert.SerializeObject(parameters);
              var content = new StringContent(jsonDictionary, Encoding.UTF8, &quot;application/json&quot;);
              HttpResponseMessage response = await client.PostAsync(Url, content);
              response.EnsureSuccessStatusCode();
  
              string msgTask = await response.Content.ReadAsStringAsync();
              Console.WriteLine(msgTask);
              return msgTask;
              }
          }
      }  
  </code></pre>
  <p><em><strong>UPDATE</strong></em> I changed the format of data being sent to the API <code>var data = @&quot;{ &quot;&quot;username&quot;&quot;: &quot;&quot;TestUser900&quot;&quot;, &quot;&quot;password&quot;&quot;: &quot;&quot;Summer$21&quot;&quot;,  &quot;&quot;email&quot;&quot;: &quot;&quot;test65349@yahoo.com&quot;&quot;}&quot;; </code></p>
  <p>and added <code>var postData = JsonConvert.SerializeObject(data); var postData2 = JsonConvert.DeserializeObject(postData); </code></p>
  <p>If I use <code>Console.WriteLine(await response.Content.ReadAsStringAsync())</code> I receive the Status and User Created, but if I use <code>return response.StatusCode.ToString();</code> nothing returns</p>