Reputation: 9662
We encounter an error while redirecting an API Http GET call. I searched stackoverflow for similar issues but it doesn't seems to be like our problem.
We have a simple scenario. Our solution is a backend which serves as a "gateway" for API calls.
From our angular client, we make calls to /api/values and it must be redirected to http://example.com:8080/api/values
.
So we took a starting .NET Core API project and we try to simple make a API client call from the API Controller.
Here is the original class:
namespace WebApplication2.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
We then added the actual call to our real API as described in many tutorials. We just want to return our response as got from our API (see below for format).
// GET api/values
[HttpGet]
public async Task<String> Get()
{
string retValue = "";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://example.com:8000");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("/document/info/ab5846a1afe8ad12e46a4e60");
if (response.IsSuccessStatusCode)
{
retValue = await response.Content.ReadAsAsync<string>();
}
}
return retValue;
}
An unhandled exception occurred while processing the request.
JsonReaderException: Unexpected character encountered while parsing value: {. Path '', line 2, position 1. ReadStringValue
If I do a postman to our API with this route, here is the result:
{
"errorCode": 0,
"docId": "ab5846a1afe8ad12e46a4e60",
"status": 0
}
{
"version":{
"major":1,
"minor":1,
"build":-1,
"revision":-1,
"majorRevision":-1,
"minorRevision":-1
},
"content":{
"headers":[
{
"key":"Content-Length",
"value":[
"78"
]
},
{
"key":"Content-Type",
"value":[
"application/json"
]
}
]
},
"statusCode":200,
"reasonPhrase":"",
"headers":[
{
"key":"Date",
"value":[
"Fri, 06 Jan 2017 12:54:31 GMT"
]
}
],
"requestMessage":{
"version":{
"major":1,
"minor":1,
"build":-1,
"revision":-1,
"majorRevision":-1,
"minorRevision":-1
},
"content":null,
"method":{
"method":"GET"
},
"requestUri":"http://example.com:8000/document/info/ab5846a1afe8ad12e46a4e60",
"headers":[
{
"key":"Accept",
"value":[
"application/json"
]
}
],
"properties":{
}
},
"isSuccessStatusCode":true
}
Upvotes: 2
Views: 2500
Reputation: 247153
this line is the problem
retValue = await response.Content.ReadAsAsync<string>();
the ReadAsAsync
generic method with the string
argument is basically desarializing the HttpResponseMessage
to a string .
Use ReadAsStringAsync
instead.
retValue = await response.Content.ReadAsStringAsync();
and you should get the desired result.
Upvotes: 4
Reputation: 9662
Ok the problem was solved by defining a response Model. Such as
public class DocumentInfo
{
public int errorCode;
public string docId;
public int status;
}
And then typing as follows:
var retValue = new DocumentInfo();
retValue = await response.Content.ReadAsAsync<DocumentInfo>();
and on the method:
public async Task<DocumentInfo> Get()
Is there any way not to strongly type the returned result? If it changes from the remote API (for example if they add for example an "Owner" attribute) it will break?
Upvotes: 1