Skip to content

Using RestSharp with streaming endpoint #1672

Closed
@JannemanDev

Description

@JannemanDev

Does RestSharp v106.15.0 work with a streaming endpoint? For example Twitter has an https://api.twitter.com/2/tweets/search/stream endpoint which streams tweet(s) matching certain rule(s) created with another endpoint.
Using curl https://api.twitter.com/2/tweets/search/stream -H "Authorization: Bearer ...." this works.
I also got it to work when using WebClient in C#:

webClient = new WebClient();
webClient.Headers[HttpRequestHeader.Authorization] = $"Bearer {settings.Twitter.BearerToken}";
webClient.OpenReadCompleted += (sender, openReadCompletedEventArgs) =>
{
    Stream stream = openReadCompletedEventArgs.Result;
    using (var reader = new StreamReader(stream))
    {
        while (!reader.EndOfStream)
        {
            string json = reader.ReadLine();
            Console.WriteLine(json);
        }
    }
};
webClient.OpenReadAsync(new Uri(settings.Twitter.FilteredStreamUrl.AbsoluteUri));

But I can't get it working using RestSharp:

var client = new RestClient($"{settings.Twitter.FilteredStreamUrl}");
var request = new RestRequest(RestSharp.Method.GET);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + settings.Twitter.BearerToken);
//request.AdvancedResponseWriter = (stream, statuscode) =>
request.ResponseWriter = (stream) =>
{
    using (var reader = new StreamReader(stream))
    {
        while (!reader.EndOfStream)
        {
            //Console.WriteLine(statuscode);
            string json = reader.ReadLine();
            Console.WriteLine(json);
        }
    }
};
client.Execute(request);

Does RestSharp support streaming endpoints? Or am I doing something wrong in my code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions