0

I need to convert String response to Json response. But I have to use different way to fix my issue but I couldn't able to find any good solution for that.

if (response.IsSuccessStatusCode)
{
    string successResponse = "Order Created Successfully with InterfaceRecordId : " + order.WMWDATA.Receipts.Receipt.InterfaceRecordId + " & ReceiptId : " +
      order.WMWDATA.Receipts.Receipt.ReceiptId;
    _logger.LogInformation(successResponse);
    return StatusCode((int)response.StatusCode, successResponse);
}
_logger.LogError(jsonResponse);
return StatusCode((int)response.StatusCode, jsonResponse);

I just need to send this successRespose as JSON response. Can you please help me to resolve this.

haldo
  • 14,512
  • 5
  • 46
  • 52
  • BTW, a string is already a valid JSON object, so what error are you having defining a `jsonReponse` string variable? – OneCricketeer Feb 18 '22 at 17:55
  • @OneCricketeer no I have checked in the Postman. It is sending text format response. So do you have any idea. – user17870723 Feb 18 '22 at 17:58
  • @OneCricketeer, It is not sending any errors , but response is coming as text. So i need to get json format in postman – user17870723 Feb 18 '22 at 18:01
  • 1) You need to return a header for `application/json` 2) You may want to try returning a Dictionary object rather than just a string if you want an object of key value pairs – OneCricketeer Feb 18 '22 at 18:04
  • You may want to read solutions at https://stackoverflow.com/questions/42360139/asp-net-core-return-json-with-status-code – OneCricketeer Feb 18 '22 at 18:06
  • @OneCricketeer I need to send this (Shipment Order Created Successfully with InterfaceRecordId : 03 & ShipmentId : SHIPTESTCS020202) one as Json response.If you can send me code. Little bit hard to understand above solution. Previously I used that way. Can u please put code – user17870723 Feb 18 '22 at 18:12
  • That itself isn't json, though, so seems you may want to learn more about what that actually means – OneCricketeer Feb 19 '22 at 00:48

1 Answers1

1

Please have a read over Format response data in ASP.NET Core Web API. There are multiple possibilities, depending on what your exact requirements are.

The simplest option is to add [Produces] attribute to the controller action, specifying the type that's returned from the aciton:

[Produces("application/json")]
public IActionResult YourAction(int id) { ... }

The above action will return application/json even when the return type of the action is string.


However, it looks like you're returning two values in the string (InterfaceRecordId and ReceiptId). You can use built-in methods (such as Ok(), BadRequest(), Created(), NotFound(), etc) which convert the object to JSON and return a specific status code.

I would recommend returning an object containing both values, and use a convenience return method (Created() or CreatedAtAction()). The following returns an anonymous object and status code 201:

var successResponse = new 
{ 
    InterfaceRecordId = order.WMWDATA.Receipts.Receipt.InterfaceRecordId,
    ReceiptId = order.WMWDATA.Receipts.Receipt.ReceiptId
}

return CreatedAtAction(nameof(YourAction), successResponse); 

The CreatedAtAction (and Created) convenience methods return a 201 (created) HTTP Code - so the client will be able to infer that from the status code. And since it's returning an object (rather than a string), the return type defaults to application/json, so there's no need for the [Produces] attribute.

haldo
  • 14,512
  • 5
  • 46
  • 52