I can't get my dotnet web api (v. 5.0) to read the json response from another API. It insists on 'decorating' the string with \ and removing quotes. I suspect it assumes the returned content is in a certain format and then tries to 'deserialize' the json content into a string.
My API returns this
{
"result": [
{
"CampaignId": 1
}
]
}
According to postman, the returned raw data is {"result":[{"CampaignId":1}]}
Which seems reasonable.
Problem is, responseContent
in the following code
var bodyStr = body.ToString();
var client = new HttpClient();
var httpContent = new StringContent(bodyStr, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://anotherapi/query",httpContent);
var responseContent = await response.Content.ReadAsStringAsync();
returns
"{\"result\":[{CampaignId:1}]}"
which obviously fails when reading it as json
using (JsonDocument document = JsonDocument.Parse(responseContent)) {}
EDIT:
I get the following exception, when calling JsonDocument.Parse(responseContent)
:
System.Text.Json.JsonReaderException: 'C' is an invalid start of a property name. Expected a '"'. LineNumber: 0 | BytePositionInLine: 12.
because ReadAsStringAsync()
seems to remove the quotes around "CampaignId", which makes the json invalid.
EDIT2: This is the returned response from the other API, according to postman:
This is the response according to curl:
$ curl --data "@/somedata.txt" -X POST -H "Content-Type: application/json" http://host/query
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 19847 100 30 100 19817 30 19817 0:00:01 0:00:01 --:--:-- 16083{"result":[{"CampaignId":1}]}
I don't see any problems with the response header or content. Everything seems fine with the returned content and "campaignId" includes the quotes.