Reputation: 599
Could anyone explain why I am getting different responses from external http API via Postman and .NetCore web Api HttpClient.
Here is a code
string westLakeApiUrl = "https://external.api.example";
HttpClient client = new HttpClient();
HttpContent content = new StringContent("");
content.Headers.ContentType = new MediaTypeHeaderValue("text/json");
var response = await client.PostAsync(westLakeApiUrl, content);
var responseContent = await response.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(responseContent);
return new OkObjectResult(data);
the result
{"totalRecords":86,"requestSucceeded":true,"countryList":[{"countryISOCode":"CW","countryName":"Curaçao","localCountryName":"Curaçao","countryId":77},{"countryISOCode":"CZ","countryName":"Czech/Slovak Republic","localCountryName":"Czech/Slovak Republic","countryId":44},{"countryISOCode":"GR","countryName":"Greece/Cyprus","localCountryName":"Greece/Cyprus","countryId":27},{"countryISOCode":"AE","countryName":"Gulf","localCountryName":"Gulf","countryId":32},{"countryISOCode":"EA","countryName":"SE Asia","localCountryName":"SE Asia","countryId":61},{"countryISOCode":"RS","countryName":"Serbia","localCountryName":"Serbia","countryId":87},{"countryISOCode":"UK","countryName":"United Kingdom","localCountryName":"UK","countryId":15},{"countryISOCode":"AU","countryName":"Australia","localCountryName":"Австралия","countryId":19},{"countryISOCode":"AT","countryName":"Austria","localCountryName":"Австрия","countryId":22},{"countryISOCode":"AZ","countryName":"Azerbaijan","localCountryName":"Азербайджан","countryId":72},{"countryISOCode":"AR","countryName":"Argentina","localCountryName":"Аргентина","countryId":60},{"countryISOCode":"AM","countryName":"Armenia","localCountryName":"Армения","countryId":71},{"countryISOCode":"BD","countryName":"Bangladesh","localCountryName":"Бангладеш","countryId":73},{"countryISOCode":"BY","countryName":"Belarus","localCountryName":"Беларусь","countryId":74},{"countryISOCode":"BE","countryName":"Belgium","localCountryName":"Бельгия","countryId":6},{"countryISOCode":"BG","countryName":"Bulgaria","localCountryName":"Болгария","countryId":75},{"countryISOCode":"BO","countryName":"Bolivia","localCountryName":"Боливия","countryId":95},{"countryISOCode":"BR","countryName":"Brazil","localCountryName":"Бразилия","countryId":49},{"countryISOCode":"HU","countryName":"Hungary","localCountryName":"Венгрия","countryId":58},{"countryISOCode":"VE","countryName":"Venezuela","localCountryName":"Венесуэла","countryId":92},{"countryISOCode":"VN","countryName":"Vietnam","localCountryName":"Вьетнам","countryId":39},{"countryISOCode":"GT","countryName":"Guatemala","localCountryName":"Гватемала","countryId":63},{"countryISOCode":"DE","countryName":"Germany","localCountryName":"Германия","countryId":16},{"countryISOCode":"HN","countryName":"Honduras","localCountryName":"Гондурас","countryId":79},{"countryISOCode":"HK","countryName":"Hong Kong","localCountryName":"Гонконг","countryId":28},{"countryISOCode":"DK","countryName":"Denmark","localCountryName":"Дания","countryId":21},{"countryISOCode":"DO","countryName":"Republica Dominicana","localCountryName":"Доминиканская Республика","countryId":67},{"countryISOCode":"EG","countryName":"Egypt","localCountryName":"Египт","countryId":35},{"countryISOCode":"IL","countryName":"Israel","localCountryName":"Израиль","countryId":36},{"countryISOCode":"IN","countryName":"India","localCountryName":"Индия","countryId":31},{"countryISOCode":"ID","countryName":"Indonesia","localCountryName":"Индонезия","countryId":41},{"countryISOCode":"IE","countryName":"Ireland","localCountryName":"Ирландия","countryId":24},{"countryISOCode":"ES","countryName":"Spain","localCountryName":"Испания","countryId":46},{"countryISOCode":"IT","countryName":"Italy","localCountryName":"Италия","countryId":51},{"countryISOCode":"KZ","countryName":"Kazakhstan","localCountryName":"Казахстан","countryId":80},{"countryISOCode":"CA","countryName":"Canada","localCountryName":"Канада","countryId":12},{"countryISOCode":"KG","countryName":"Kyrgyzstan","localCountryName":"Киргизстан","countryId":81},{"countryISOCode":"CN","countryName":"China","localCountryName":"Китай","countryId":42},{"countryISOCode":"CO","countryName":"Colombia","localCountryName":"Колумбия","countryId":52},{"countryISOCode":"CR","countryName":"Costa Rica","localCountryName":"Коста-Рика","countryId":68},{"countryISOCode":"LV","countryName":"Latvia","localCountryName":"Латвия","countryId":82},{"countryISOCode":"LT","countryName":"Lithuania","localCountryName":"Литва","countryId":83},{"countryISOCode":"MY","countryName":"Malaysia","localCountryName":"Малайзия","countryId":30},{"countryISOCode":"MX","countryName":"Mexico","localCountryName":"Мексика","countryId":4},{"countryISOCode":"MN","countryName":"Mongolia","localCountryName":"Монголия","countryId":84},{"countryISOCode":"NP","countryName":"Nepal","localCountryName":"Непал","countryId":85},{"countryISOCode":"NL","countryName":"Netherlands","localCountryName":"Нидерланды","countryId":10},{"countryISOCode":"NI","countryName":"Nicaragua","localCountryName":"Никарагуа","countryId":86},{"countryISOCode":"NZ","countryName":"New Zealand","localCountryName":"Новая Зеландия","countryId":14},{"countryISOCode":"NO","countryName":"Norway","localCountryName":"Норвегия","countryId":25},{"countryISOCode":"PA","countryName":"Panama","localCountryName":"Панама","countryId":64},{"countryISOCode":"PY","countryName":"Paraguay","localCountryName":"Парагвай","countryId":65},{"countryISOCode":"PE","countryName":"Peru","localCountryName":"Перу","countryId":55},{"countryISOCode":"PL","countryName":"Poland","localCountryName":"Польша","countryId":43},{"countryISOCode":"PT","countryName":"Portugal","localCountryName":"Португалия","countryId":57},{"countryISOCode":"PR","countryName":"Puerto Rico","localCountryName":"Пуэрто-Рико","countryId":66},{"countryISOCode":"RU","countryName":"Russia","localCountryName":"Россия","countryId":48},{"countryISOCode":"RO","countryName":"Romania","localCountryName":"Румыния","countryId":45},{"countryISOCode":"SV","countryName":"El Salvador","localCountryName":"Сальвадор","countryId":78},{"countryISOCode":"SA","countryName":"Saudi Arabia","localCountryName":"Саудовская Аравия","countryId":33},{"countryISOCode":"SG","countryName":"Singapore","localCountryName":"Сингапур","countryId":11},{"countryISOCode":"SI","countryName":"Slovenia","localCountryName":"Словения","countryId":93},{"countryISOCode":"US","countryName":"United States","localCountryName":"Соединенные Штаты","countryId":17},{"countryISOCode":"TJ","countryName":"Tajikistan","localCountryName":"Таджикистан","countryId":89},{"countryISOCode":"TH","countryName":"Thailand","localCountryName":"Таиланд","countryId":38},{"countryISOCode":"TW","countryName":"Taiwan","localCountryName":"Тайвань","countryId":29},{"countryISOCode":"TT","countryName":"Trinidad y Tobago","localCountryName":"Тринидад и Тобаго","countryId":70},{"countryISOCode":"TM","countryName":"Turkmenistan","localCountryName":"Туркменистан","countryId":90},{"countryISOCode":"TR","countryName":"Turkey","localCountryName":"Турция","countryId":34},{"countryISOCode":"UZ","countryName":"Uzbekistan","localCountryName":"Узбекистан","countryId":91},{"countryISOCode":"UA","countryName":"Ukraine","localCountryName":"Украина","countryId":47},{"countryISOCode":"UY","countryName":"Uruguay","localCountryName":"Уругвай","countryId":94},{"countryISOCode":"PH","countryName":"Philippines","localCountryName":"Филиппины","countryId":40},{"countryISOCode":"FI","countryName":"Finland","localCountryName":"Финляндия","countryId":3},{"countryISOCode":"FR","countryName":"France","localCountryName":"Франция","countryId":26},{"countryISOCode":"HR","countryName":"Croatia","localCountryName":"Хорватия","countryId":76},{"countryISOCode":"CL","countryName":"Chile","localCountryName":"Чили","countryId":56},{"countryISOCode":"CH","countryName":"Switzerland","localCountryName":"Швейцария","countryId":18},{"countryISOCode":"SE","countryName":"Sweden","localCountryName":"Швеция","countryId":9},{"countryISOCode":"LK","countryName":"Sri Lanka","localCountr
But Postman returns full valid json result.
{"totalRecords":86,"requestSucceeded":true,"countryList":[{"countryISOCode":"CW","countryName":"Curaçao","localCountryName":"Curaçao","countryId":77},{"countryISOCode":"CZ","countryName":"Czech/Slovak Republic","localCountryName":"Czech/Slovak Republic","countryId":44},{"countryISOCode":"GR","countryName":"Greece/Cyprus","localCountryName":"Greece/Cyprus","countryId":27},{"countryISOCode":"AE","countryName":"Gulf","localCountryName":"Gulf","countryId":32},{"countryISOCode":"EA","countryName":"SE Asia","localCountryName":"SE Asia","countryId":61},{"countryISOCode":"RS","countryName":"Serbia","localCountryName":"Serbia","countryId":87},{"countryISOCode":"UK","countryName":"United Kingdom","localCountryName":"UK","countryId":15},{"countryISOCode":"AU","countryName":"Australia","localCountryName":"Австралия","countryId":19},{"countryISOCode":"AT","countryName":"Austria","localCountryName":"Австрия","countryId":22},{"countryISOCode":"AZ","countryName":"Azerbaijan","localCountryName":"Азербайджан","countryId":72},{"countryISOCode":"AR","countryName":"Argentina","localCountryName":"Аргентина","countryId":60},{"countryISOCode":"AM","countryName":"Armenia","localCountryName":"Армения","countryId":71},{"countryISOCode":"BD","countryName":"Bangladesh","localCountryName":"Бангладеш","countryId":73},{"countryISOCode":"BY","countryName":"Belarus","localCountryName":"Беларусь","countryId":74},{"countryISOCode":"BE","countryName":"Belgium","localCountryName":"Бельгия","countryId":6},{"countryISOCode":"BG","countryName":"Bulgaria","localCountryName":"Болгария","countryId":75},{"countryISOCode":"BO","countryName":"Bolivia","localCountryName":"Боливия","countryId":95},{"countryISOCode":"BR","countryName":"Brazil","localCountryName":"Бразилия","countryId":49},{"countryISOCode":"HU","countryName":"Hungary","localCountryName":"Венгрия","countryId":58},{"countryISOCode":"VE","countryName":"Venezuela","localCountryName":"Венесуэла","countryId":92},{"countryISOCode":"VN","countryName":"Vietnam","localCountryName":"Вьетнам","countryId":39},{"countryISOCode":"GT","countryName":"Guatemala","localCountryName":"Гватемала","countryId":63},{"countryISOCode":"DE","countryName":"Germany","localCountryName":"Германия","countryId":16},{"countryISOCode":"HN","countryName":"Honduras","localCountryName":"Гондурас","countryId":79},{"countryISOCode":"HK","countryName":"Hong Kong","localCountryName":"Гонконг","countryId":28},{"countryISOCode":"DK","countryName":"Denmark","localCountryName":"Дания","countryId":21},{"countryISOCode":"DO","countryName":"Republica Dominicana","localCountryName":"Доминиканская Республика","countryId":67},{"countryISOCode":"EG","countryName":"Egypt","localCountryName":"Египт","countryId":35},{"countryISOCode":"IL","countryName":"Israel","localCountryName":"Израиль","countryId":36},{"countryISOCode":"IN","countryName":"India","localCountryName":"Индия","countryId":31},{"countryISOCode":"ID","countryName":"Indonesia","localCountryName":"Индонезия","countryId":41},{"countryISOCode":"IE","countryName":"Ireland","localCountryName":"Ирландия","countryId":24},{"countryISOCode":"ES","countryName":"Spain","localCountryName":"Испания","countryId":46},{"countryISOCode":"IT","countryName":"Italy","localCountryName":"Италия","countryId":51},{"countryISOCode":"KZ","countryName":"Kazakhstan","localCountryName":"Казахстан","countryId":80},{"countryISOCode":"CA","countryName":"Canada","localCountryName":"Канада","countryId":12},{"countryISOCode":"KG","countryName":"Kyrgyzstan","localCountryName":"Киргизстан","countryId":81},{"countryISOCode":"CN","countryName":"China","localCountryName":"Китай","countryId":42},{"countryISOCode":"CO","countryName":"Colombia","localCountryName":"Колумбия","countryId":52},{"countryISOCode":"CR","countryName":"Costa Rica","localCountryName":"Коста-Рика","countryId":68},{"countryISOCode":"LV","countryName":"Latvia","localCountryName":"Латвия","countryId":82},{"countryISOCode":"LT","countryName":"Lithuania","localCountryName":"Литва","countryId":83},{"countryISOCode":"MY","countryName":"Malaysia","localCountryName":"Малайзия","countryId":30},{"countryISOCode":"MX","countryName":"Mexico","localCountryName":"Мексика","countryId":4},{"countryISOCode":"MN","countryName":"Mongolia","localCountryName":"Монголия","countryId":84},{"countryISOCode":"NP","countryName":"Nepal","localCountryName":"Непал","countryId":85},{"countryISOCode":"NL","countryName":"Netherlands","localCountryName":"Нидерланды","countryId":10},{"countryISOCode":"NI","countryName":"Nicaragua","localCountryName":"Никарагуа","countryId":86},{"countryISOCode":"NZ","countryName":"New Zealand","localCountryName":"Новая Зеландия","countryId":14},{"countryISOCode":"NO","countryName":"Norway","localCountryName":"Норвегия","countryId":25},{"countryISOCode":"PA","countryName":"Panama","localCountryName":"Панама","countryId":64},{"countryISOCode":"PY","countryName":"Paraguay","localCountryName":"Парагвай","countryId":65},{"countryISOCode":"PE","countryName":"Peru","localCountryName":"Перу","countryId":55},{"countryISOCode":"PL","countryName":"Poland","localCountryName":"Польша","countryId":43},{"countryISOCode":"PT","countryName":"Portugal","localCountryName":"Португалия","countryId":57},{"countryISOCode":"PR","countryName":"Puerto Rico","localCountryName":"Пуэрто-Рико","countryId":66},{"countryISOCode":"RU","countryName":"Russia","localCountryName":"Россия","countryId":48},{"countryISOCode":"RO","countryName":"Romania","localCountryName":"Румыния","countryId":45},{"countryISOCode":"SV","countryName":"El Salvador","localCountryName":"Сальвадор","countryId":78},{"countryISOCode":"SA","countryName":"Saudi Arabia","localCountryName":"Саудовская Аравия","countryId":33},{"countryISOCode":"SG","countryName":"Singapore","localCountryName":"Сингапур","countryId":11},{"countryISOCode":"SI","countryName":"Slovenia","localCountryName":"Словения","countryId":93},{"countryISOCode":"US","countryName":"United States","localCountryName":"Соединенные Штаты","countryId":17},{"countryISOCode":"TJ","countryName":"Tajikistan","localCountryName":"Таджикистан","countryId":89},{"countryISOCode":"TH","countryName":"Thailand","localCountryName":"Таиланд","countryId":38},{"countryISOCode":"TW","countryName":"Taiwan","localCountryName":"Тайвань","countryId":29},{"countryISOCode":"TT","countryName":"Trinidad y Tobago","localCountryName":"Тринидад и Тобаго","countryId":70},{"countryISOCode":"TM","countryName":"Turkmenistan","localCountryName":"Туркменистан","countryId":90},{"countryISOCode":"TR","countryName":"Turkey","localCountryName":"Турция","countryId":34},{"countryISOCode":"UZ","countryName":"Uzbekistan","localCountryName":"Узбекистан","countryId":91},{"countryISOCode":"UA","countryName":"Ukraine","localCountryName":"Украина","countryId":47},{"countryISOCode":"UY","countryName":"Uruguay","localCountryName":"Уругвай","countryId":94},{"countryISOCode":"PH","countryName":"Philippines","localCountryName":"Филиппины","countryId":40},{"countryISOCode":"FI","countryName":"Finland","localCountryName":"Финляндия","countryId":3},{"countryISOCode":"FR","countryName":"France","localCountryName":"Франция","countryId":26},{"countryISOCode":"HR","countryName":"Croatia","localCountryName":"Хорватия","countryId":76},{"countryISOCode":"CL","countryName":"Chile","localCountryName":"Чили","countryId":56},{"countryISOCode":"CH","countryName":"Switzerland","localCountryName":"Швейцария","countryId":18},{"countryISOCode":"SE","countryName":"Sweden","localCountryName":"Швеция","countryId":9},{"countryISOCode":"LK","countryName":"Sri Lanka","localCountryName":"Шри-Ланка","countryId":88},{"countryISOCode":"EC","countryName":"Ecuador","localCountryName":"Эквадор","countryId":62},{"countryISOCode":"EE","countryName":"Estonia","localCountryName":"Эстония","countryId":96},{"countryISOCode":"ZA","countryName":"South Africa","localCountryName":"Южная Африка","countryId":37},{"countryISOCode":"KR","countryName":"Korea","localCountryName":"Южная Корея","countryId":23},{"countryISOCode":"JM","countryName":"Jamaica","localCountryName":"Ямайка","countryId":69},{"countryISOCode":"JP","countryName":"Japan","localCountryName":"Япония","countryId":20}]}
Why the result from .NetCore HttpClient is partial?
I've already tried options like specifying request headers:
content.Headers.ContentType.CharSet = "UTF-8";
content.Headers.Add("Strict-Transport-Security", new string[]{"max-age=63072000", "includeSubdomains"});
content.Headers.Add("Access-Control-Allow-Origin", new string[]{"*"});
content.Headers.Add("Access-Control-Allow-Methods", new string[]{"POST", "GET"});
content.Headers.Add("Access-Control-Allow-Headers", new string[]{"accountId","accessKey","Content-Type","Content-Length","Accept,Origin","Referer","User-Agent"});
content.Headers.Add("Access-Control-Max-Age", new string[]{"86400"});
content.Headers.Add("Keep-Alive", new string[]{"timeout=5, max=100"});
content.Headers.ContentLength = 0;
also added:
client.DefaultRequestHeaders.ExpectContinue = false;
Didn't help.
Upvotes: 1
Views: 1868
Reputation: 599
The problem was on 3rd party API side. The owner of API wrong calculates response ContentLenght. The calculation algorithm was not based on UTF8 encoding. So, the ContentLenght header in response cuts part of the response body. Hope it would be helpful for someone!
Upvotes: 2