title: Constant BadRequest when posting JSON to endpoint using HttpClient
body: <p>I am struggeling to consume an API endpoint (<a href="http://ogre.adc4gis.com/convertJson" rel="nofollow noreferrer">http://ogre.adc4gis.com/convertJson</a>) that takes JSON data and returns a zip file. Visiting <a href="http://ogre.adc4gis.com" rel="nofollow noreferrer">http://ogre.adc4gis.com</a> shows the parameters the api expects.</p>
  
  <p>It absolutely works using Postman and using Javascript, so I am wondering what I am doing wrong with in my c# code.</p>
  
  <p>This an example JSON string, which can be posted to the API:</p>
  
  <pre><code>{
      "displayFieldName": "NUM_GES2_1",
      "fieldAliases": {
          "NUM_GES2_1": "NUM_GES2_1"
      },
      "geometryType": "esriGeometryPoint",
      "spatialReference": {
          "wkid": 102362,
          "latestWkid": 4647
      },
      "fields": [{
          "name": "NUM_GES2_1",
          "type": "esriFieldTypeString",
          "alias": "NUM_GES2_1",
          "length": 254
      }],
      "features": [{
          "attributes": {
              "NUM_GES2_1": "001-08"
          },
          "geometry": {
              "x": 32674408.2009,
              "y": 5790291.4659000002
          }
      }]
  }
  </code></pre>
  
  <p>For completenes sake, this is the action method, that invokes the call to the bespoke api:</p>
  
  <pre><code>[HttpGet]
  [Route("{id:int}/Attributive")]
  public async Task&lt;IActionResult&gt; GetFeatureClass(int id)
  {
      var client = new HttpClient();
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      //get token from internal api
      var token = await _tokenService.RefreshToken();
      //dummy address that returns a single object for development
      var tokenObjString = await client.GetStringAsync(
          $"http://url.to/MapServer/{id}/query?where=1%3D1&amp;objectIds=4&amp;f=pjson&amp;token={token}"
      );
  
      var data = await PostDataToOgrService(tokenObjString);
  
      var response = File(data, "application/octet-stream", "FeatureClass.Zip");
      return response;
  }
  </code></pre>
  
  <p>Here comes the part where the actual call to the api is made.
  Payload is the escaped json string. The get rid of all the escapes, I've just converted it to an object and then back again.</p>
  
  <pre><code>public async Task&lt;byte[]&gt; PostDataToOgrService(string payload)
  {
      var client = new HttpClient();
      var newJson = JsonConvert.DeserializeObject(payload);
      var pairs = new Dictionary&lt;string, object&gt;
      {
          { "json", newJson }
      };
      var json = JsonConvert.SerializeObject(pairs);
      var content = new StringContent(json, Encoding.UTF8, "application/json");
  
      var response = await client.PostAsync("http://ogre.adc4gis.com/convertJson", content);
  
      if (response.IsSuccessStatusCode)
      {
          return await response.Content.ReadAsByteArrayAsync();
      }
      else
      {
          throw new Exception("Something went wrong");
      }
  }
  </code></pre>
  
  <p>Every call returns an Error 400 Bad Request:</p>
  
  <pre><code>{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.NoWriteNoSeekStreamContent, Headers:
  {
    Date: Fri, 08 Sep 2017 06:15:01 GMT
    ETag: W/"27-Ag3Jnk3T/v6dECAccJTzg4aO/wA"
    X-Powered-By: Express
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST
    Access-Control-Allow-Headers: X-Requested-With
    Access-Control-Expose-Headers: Content-Disposition
    Content-Length: 39
    Content-Type: application/json; charset=utf-8
  }}
  </code></pre>
  
  <p>Things I've tried:</p>
  
  <p>Do not convert the payload to an object and just use it as it is:</p>
  
  <pre><code>var pairs = new Dictionary&lt;string, string&gt;
  {
      { "json", payload }
  };
  var json = JsonConvert.SerializeObject(pairs);
  var content = new StringContent(json, Encoding.UTF8, "application/json");
  </code></pre>
  
  <p>Build the string content raw:</p>
  
  <pre><code>payload = "{ \"json\": " + payload + "}";
  </code></pre>
  
  <p>Use "application/x-www-url-formencoded" as Content-Type</p>
  
  <p>I am a total loss here. What am I doing wrong?</p>
  
  <p><strong>EDIT</strong>
  As requested, here are the request headers from a successfull postman request:</p>
  
  <pre><code>POST /convertJson HTTP/1.1
  cache-control: no-cache
  Postman-Token: 2976abca-6725-43ce-873e-907c12a9fdee
  Content-Type: multipart/form-data; boundary=--------------------------829328978588990941765750
  User-Agent: PostmanRuntime/6.1.6
  Accept: */*
  Host: ogre.adc4gis.com
  accept-encoding: gzip, deflate
  content-length: 629
  Connection: keep-alive
  </code></pre>