'How to send status code with message .NET Core Web API
I am working with two Web API projects. I am getting response from one API to another API with status code and message. Using following method I need to send status code with message.
[HttpPost]
public async Task<string> CreateOrder(Order order)
{
string jsonResponse = null;
HttpClient client = new();
var payload = new JsonPayloadBody();
payload.Api = new Api();
payload.Header = new OrderHeader();
payload.Api.NAME = "CREATE";
payload.Api.QUERY = "CREATE_ORDER";
payload.Header.INTERFACE_RECORD_ID = order.INTERFACE_RECORD_ID;
payload.Header.INTERFACE_ACTION_CODE = order.INTERFACE_ACTION_CODE;
payload.LineItems.Add(li); }
var orderPayload = JsonConvert.SerializeObject(payload);
var payloadObj = JsonConvert.DeserializeObject<MHScaleMessage>(orderPayload);
var requestPayload = payloadObj.ToJsonString();
var stringContent = new StringContent(requestPayload, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(_baseUrl, stringContent);
if (response.IsSuccessStatusCode)
{
jsonResponse = await response.Content.ReadAsStringAsync();
_logger.LogInformation(jsonResponse);
_logger.LogInformation("Order created successfully");
}
else
{
_logger.LogInformation("Order creation filed");
}
return jsonResponse;
}
Using this line
jsonResponse = await response.Content.ReadAsStringAsync();
I was able to send message to postman but it's sending 200 status code always. So I need to send status code according to the response, for example:
return NotFound
return Ok
return BadRequest
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
