3

Here if the response is success I want to return JSON responseStr with status code System.Net.HttpStatusCode.Accepted. But using this code I am able to return string. But I want to return Json response.

public async Task<IActionResult> Receipt()
      {
            var response = await api.Get(string.Format(url));
            var responseStr = await response.Content.ReadAsStringAsync();
            if (response.IsSuccessStatusCode)
            {
                return StatusCode((int)System.Net.HttpStatusCode.Accepted, responseStr);               
            }
            else
            {
                _logger.LogError(responseStr);
                return BadRequest(responseStr);
            }
       }
1

1 Answer 1

1

Ordinarily a 202 / Accepted response wouldn't contain a body in the response.

But you can achieve this by either of the following:

return Accepted(responseStr);

Note that this will return the responseStr field as a string - because it is a string.

If you want to return it as a json object - you could deserialise it first and return the object in the same manner:

var asObject = JsonConvert.DeserializeObject<Type>(responseStr);
return Accepted (asObject);

Also, you should move the ReadAsStringAsync call to inside the if (Successful) {} block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.