0

I'm trying to return a JSON object retrieved from a 3rd party API.

[Route("api/Catalog/Categories")]
public class CategoriesController : Controller
{
    //Get all categories
    [HttpGet]
    public IActionResult Get()
    {

        var client = new RestClient();
        client.BaseUrl = new Uri("http://api.tcgplayer.com");

        var request = new RestRequest(Method.GET);
        request.Resource = "/catalog/categories";
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Content-Type", "application/json; charset=utf-8");
        request.AddHeader("Authorization", "Bearer redacted");

        var tcgResponse = client.Execute(request);

        return Ok(tcgResponse.Content);
    }
}

The content type shows as "document", which is not desirable. How do specify the content type as "application/json"?

chrome network tab

chrome response headers

I have already tried adding

[Produces("application/json")]

but this caused double serialization of my response content.

Simply Ged
  • 6,639
  • 9
  • 29
  • 37
Jonathan
  • 11
  • 2
  • why don't you return json(tcgResponse.Content)? also, you can deserialize the content and return the entity(and return type of method would be your entity type, not IActionResult ) – Mohammad Reza Farahani Sep 02 '18 at 06:06
  • My thinking is that I need the JSON object to be returned by Web API so deserializing seem's redundant. I could be wrong. – Jonathan Sep 02 '18 at 22:21
  • For **Return type**, it depends on `API`, share us a screen shot about the `tcgResponse.Content`, try to use postman to request `/catalog/categories`, and share us the response. Is your image for request from `3rd party api` or `CategoriesController`, you may tryo check the content. In general, it returns `document` is a plantext html. Try replace `return Ok(tcgResponse.Content);` with `return Ok(new { Id = 1, Name = "Test" });`, will you get expected json? – Edward Sep 03 '18 at 02:38

1 Answers1

0

FTR I ended up deserializing and reserializing for the return statement. Not pretty, but it works.

 var tcgResponse = client.Execute(request);

 var r = JsonConvert.DeserializeObject<dynamic>(tcgResponse.Content);

 return r;
Jonathan
  • 11
  • 2