0

I am not getting any response - no error, no bad request status, etc. - when I send a post request to this API route. postData is simply a JSON object. The funny thing here is this: When i send post data as a string instead of an object, I can get a response.

View the code below:

        [HttpPost]
        [Route("api/updateStaffs/")]
        public async Task<object> UpdateStaff([FromBody] object postData)
        {
            string _apiUrl = "http://localhost:5000/system/getToken";
            string _baseAddress = "http://localhost:5000/system/getToken";

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_baseAddress);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                var responseMessage = await client.PostAsync(_apiUrl, new StringContent(postData.ToString(), Encoding.UTF8, "application/json"));

                if (responseMessage.IsSuccessStatusCode)
                {
                    var response = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = responseMessage.Content;
                    return ResponseMessage(response);
                }
            }
            return NotFound();
       }

No response:

var postData = new {
    user = "test"
    pass = "hey"
};

var responseMessage = await client.PostAsync(_apiUrl, new StringContent(postData.ToString(), Encoding.UTF8, "application/json"));

OR

var responseMessage = await client.PostAsync(_apiUrl, new StringContent("{}", Encoding.UTF8, "application/json"));

Will get response:

var responseMessage = await client.PostAsync(_apiUrl, new StringContent("blahblah", Encoding.UTF8, "application/json"));

The receiving API is a third-party application so I am unable to verify if this error is on the other end.

Thanks.

  • When you say 'no response', do you mean that `responseMessage` is null? Is there a chance that an exception is being thrown and you're not being alerted to it? – David Osborne Aug 17 '22 at 08:45
  • Have you tried using the `PostAsJsonAsync()` extension method? It seems like a better fit for what you doing and might give you some more information. – David Osborne Aug 17 '22 at 08:48
  • You can have a look at this question for more information as well, check it out [here](https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body) – Jayme Aug 17 '22 at 09:08
  • 1
    Also worth testing the request in something like Postman to see if the same behaviour happens there. – lee-m Aug 17 '22 at 09:09
  • I am actually sending the request from postman. 'No response' meaning postman is stuck at 'sending request'. I do not get any reply back. No error, status code, etc. Eventually the async task timeouts. – Thomas Anderson Aug 18 '22 at 00:44
  • PostAsJsonAsync doesn't work either. In fact, the problem occurs specifically if I send the body as a JSON. – Thomas Anderson Aug 18 '22 at 00:55
  • Sounds like this issue is most likely on the API side if postman doesn't work either – Jayme Aug 18 '22 at 06:37

2 Answers2

1

Figured out the issue. Have to use HttpVersion10 instead of HttpVersion11.

0

If you dont want to use PostAsJsonAsync

You need to serialize your anonymous type to JSON, the most common tool for this is Json.NET

var jsonData = JsonConvert.SerializeObject(postData);

Then you need to construct a content object to send this data, here we can use ByteArrayContent but you can use a different type

var buffer = System.Text.Encoding.UTF8.GetBytes(jsonData);
var byteContent = new ByteArrayContent(buffer);

Then send the request

var responseMessage = await client.PostAsync(_apiUrl, byteContent);
Jayme
  • 1,776
  • 10
  • 28