Reputation: 2167
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
Upvotes: 0
Views: 1291
Reputation: 11514
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;
...
Upvotes: 1