HttpRequestException. Error while copying content to a stream. The response ended prematurely.

Making 95 calls to https://api-eit.refinitiv.com/permid/search?q=ticker:{ticker} from a C# application in a loop we get a HttpRequestException, "Error while copying content to a stream" with inner exception "The response ended prematurely" when reading the response content. The resonse statuscode is 200 so it's not rate-limiting.
Here's some sample code to repeat, with a crude re-try that shows the issue is transient as we actually get data back for all tickers with the retry in place.
using System.Net.Http.Headers;
namespace MyApp
{
internal class Program
{
static async Task Main(string[] args)
{
try
{
var tickerList = new List<string>()
{
"AAF", "AAL", "ABF", "ADM", "AHT", "ANTO", "AUTO", "AV/", "AVV", "AZN", "BA/",
"BARC", "BATS", "BDEV", "BKG", "BLND", "BME", "BNZL", "BP/", "BRBY", "BT/A",
"CCH", "CNA", "CPG", "CRDA", "CRH", "CTEC", "DCC", "DGE", "DPH", "EDV",
"ENT", "EXPN", "FCIT", "FLTR", "FRAS", "FRES", "GLEN", "GSK", "HBR", "HL/",
"HLMA", "HLN", "HSBA", "HSV", "IAG", "ICP", "IHG", "III", "IMB", "INF",
"ITRK", "JD/", "KGF", "LAND", "LGEN", "LLOY", "LSEG", "MNDI", "MNG", "MRO",
"NG/", "NWG", "NXT", "OCDO", "PHNX", "PRU", "PSH", "PSN", "PSON", "REL",
"RIO", "RKT", "RMV", "RR/", "RS1", "RTO", "SBRY", "SDR", "SGE", "SGRO",
"SHEL", "SKG", "SMDS", "SMIN", "SMT", "SN/", "SPX", "SSE", "STAN", "STJ",
"SVT", "TSCO", "TW/", "ULVR", "UTG", "UU/", "VOD", "WPP", "WTB",
};
using var httpClient = new HttpClient();
var accessToken = "PersonalAccessTokenDetails
foreach (var ticker in tickerList)
{
var retriesAllowed=1;
do
{
var request = new HttpRequestMessage(HttpMethod.Get,
$"https://api-eit.refinitiv.com/permid/search?q=ticker:{ticker}");
request.Headers.Add("x-ag-access-token", accessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")
var response = await httpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
Console.WriteLine($"[{response?.StatusCode}] ticker:{ticker}");
if ((response is {IsSuccessStatusCode: false} or null))
break;
try
{
//Deserialize resonseBody removed for brevity
var responseBody = await response.Content.ReadAsStringAsync(
retriesAllowed = 0;
}
catch (HttpRequestException reqEx)
{
Console.WriteLine($"{ticker} failed: {reqEx.Message} [{reqEx.InnerException?.Message}] retrying...");
}
} while (retriesAllowed > 0);
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}:{ex.InnerException?.Message}");
}
finally
{
Console.WriteLine("press ENTER to exit");
Console.ReadLine();
}
}
}
}
Best Answer
-
Thank you for sharing the code. I can replicate this issue.
From my test, the server is not quite stable when handling a lot of requests. It could be this error.
127.0.0.1:58294: GET https://api-eit.refinitiv.com/permid/search?q=ticker:VOD
<< 200 OK (content missing)
<< HTTP/1 protocol error: peer closed connection without sending complete message body (incomplete chunked read)The server cut the connection while the application was reading the data. Therefore, it is a good idea to implement retrying in the code.
I will contact the product team to see what they can do regarding this issue.
0
Answers
-
The product team mentioned that it could be a network issue.
Therefore, implementing the retry logic in the code is the best way to handle this kind of issue.
0
Categories
- All Categories
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 683 Datastream
- 1.4K DSS
- 613 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 248 ETA
- 552 WebSocket API
- 37 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 627 Refinitiv Data Platform Libraries
- 5 LSEG Due Diligence
- 1 LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 26 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 191 TREP Infrastructure
- 228 TRKD
- 915 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 84 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛