Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use RestClient to call Web API in C #

Shulou Source: shulou.com Published: 2022-05-31 18:30:20 09月21日 Update

This article mainly introduces "how to use RestClient to call Web API in C#". In daily operation, I believe many people have doubts about how to use RestClient to call Web API in C#. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use RestClient to call Web API in C#". Next, please follow the editor to study!

Get Token

/ get Token / public static async Task GetToken () {var url = $"http://127.0.0.1/terminal-tenant/sys/login"; var client = new RestClient (url); var request = new RestRequest (Method.POST); request.RequestFormat = DataFormat.Json Var body = new {username = "xxx", password = "xxx"}; request.AddParameter ("application/json; charset=utf-8", JsonConvert.SerializeObject (body), ParameterType.RequestBody); IRestResponse response = client.Execute (request); var responseContent = response.Content; return responseContent;}

Save data

/ / save data / public async Task Save (Model model, string token) {var url = $"http://127.0.0.1/terminal-tenant/app/visitor/save"; var client = new RestClient (url); var request = new RestRequest (Method.POST); request.RequestFormat = DataFormat.Json Request.AddHeader ("token", token); request.AddParameter ("application/json; charset=utf-8", JsonConvert.SerializeObject (model), ParameterType.RequestBody); IRestResponse response = client.Execute (request); var responseContent = response.Content; return responseContent;}

Upload files

/ upload file / public async Task Upload (byte [] bt, string token) {var url = $"http://127.0.0.1/terminal-tenant/tenant/common/upload"; var client = new RestClient (url) Var request = new RestRequest (Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader ("token", token); request.AddFile ("file", bt, "multipart/form-data"); IRestResponse response = client.Execute (request); var responseContent = response.Content; return responseContent } at this point, the study on "how to use RestClient to call Web API in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Caching learning data files more help practical next articles methods theories knowledge articles websites materials follow questions good use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn OPPO Reno Linux MySQL