RestSharp Sending Post Request With Basic Authentication

Sending post requests with the RestSharp Library.
Patriek
23 augustus 2022:

You can send a simple post request with RestSharp by using the following code.

    public string PushTextToServer(string text)
    {
        var api = this.Endpoint;
        var client = new RestClient(api);
        var request = new RestRequest("/object/set/",Method.Post);
        client.Authenticator = new HttpBasicAuthenticator(Username, Password);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("guid",Guid.NewGuid(),ParameterType.GetOrPost);
        request.AddParameter("type", "text/post", ParameterType.GetOrPost);
        request.AddParameter("body", text, ParameterType.GetOrPost);
        RestResponse response = client.Execute(request);
        var content = response.Content; // raw content as string

        return response.StatusDescription;
    }

Make sure you are using the application/x-www-form-urlencoded content-type to mimic normal post behaviour. The client authenticator takes care of basic authentication for you. The function returns the StatusDescription to determine whether your request was succesfull.

  • Comment was deleted due to spam!
  • Comment was deleted due to spam!
I like to see who's visiting my website, do you accept?
Do you want to accept tracking cookies?