0

I am building a Web API which makes a asynchronous call to the Odata endpoint. The odata endpoint returns JSON, and I am trying to return the same JSON from the Web API too. Since I am calling the Odata endpoint asynchronously I am using Task as return type my API method but I want to return them as JSON and I am not sure how I can do that.Below is my code

   public async Task<string> GetEmployee(string instance)
    {
       .....
        EmployeeDTO.RootObject returnObj = new EmployeeDTO.RootObject();
        var responsedata = "";
        try
        {
        using (var client_Core = new HttpClient())
            {
               ....
                string core_URL = BaseURL_Core+URL_instance;
                var response = client_Core.GetAsync(core_URL).Result;

                responsedata = await response.Content.ReadAsStringAsync();
              }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return responsedata;

Currently it returns like

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
 {
       "@odata.context":"https://science.com/odata/$metadata#EMPLOYEE",
"value":[
       { 
        "Id":5000004, 
        "Name":"Account", 
        "Barcode":"EM1"
       }]
    }
 </string>

I am trying to avoid the string tag around the JSON response. How acn we do that

enter image description here

trx
  • 2,077
  • 9
  • 48
  • 97
  • What's the content type you requested? Web API's responses are converted to the requested format if they're objects and one of the requested formats is supported. – Chayim Friedman Aug 20 '18 at 12:12
  • Why does ASP.NET MVC belong to this question? – Chayim Friedman Aug 20 '18 at 12:12
  • If the service only returns xml then you could simply parse the response as xml and extract the json from the `` element. Incidentally, you don't need to block with `.Result` if you are in an async method, you should `await`. Also [try not to new up an `HttpClient` for each request](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) – Crowcoder Aug 20 '18 at 12:29
  • @Crowcoder Thanks for you suggestion on the HttpClient I will fix that. I am not doing anyother manipulation in my API and the other application that will call my API can only consume JSON. How can I have a JSON response. Should I change the `Task` anyother format? – trx Aug 20 '18 at 12:50
  • @ChayimFriedman I havenot added the content type in my API, where should I be adding that? – trx Aug 20 '18 at 12:54
  • I think that the default is XML. Try to set the content type to `application/json`. – Chayim Friedman Aug 20 '18 at 12:55
  • If it won't work, see https://stackoverflow.com/questions/18266952/asp-net-web-api-returning-xml-instead-of-json. This is a similar problem. – Chayim Friedman Aug 20 '18 at 13:02

1 Answers1

1

I hope the API can return JSON and it is just a matter of changing the Content-Type as suggested by Chayim.

But if it will only return xml, you could do this:

...
string core_URL = BaseURL_Core+URL_instance;
var response = await client_Core.GetAsync(core_URL);
string xml = await response.Content.ReadAsStringAsync();
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(xml);
responsedata = doc.Root.Value;
...
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • If I am not using `.result` in `await client_Core.GetAsync(core_URL)` I am getting error in the below line like `'System.Threading.Tasks.Task' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'System.Threading.Tasks.Task' could be found (are you missing a using directive or an assembly reference?)` – trx Aug 20 '18 at 19:37
  • Are you sure you are using `await`? That is the error you would get if you omitted the `await` keyword. – Crowcoder Aug 20 '18 at 19:52
  • @trx Thank you. You did **not** add `await` to `client_Core.GetAsync(core_URL)` – Crowcoder Aug 20 '18 at 20:20
  • Getting an error while Parsing `$exception {"Data at the root level is invalid. Line 1, position 1."} System.Exception {System.Xml.XmlException}` – trx Aug 20 '18 at 20:26
  • @trx That code worked for me based on the xml in your original question. I see you asked another question and your system has now been modified so I don't know the current state of the response. What exactly is the string value of `response.Content.ReadAsStringAsync()` now? – Crowcoder Aug 20 '18 at 20:29