Hi Folks,
How to write Rest Client with get, post , put and delete method using C# (Web Api)
Thanks,
Saravanakumar
Hi Folks,
How to write Rest Client with get, post , put and delete method using C# (Web Api)
Thanks,
Saravanakumar
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Jul 15, 2022, 5:55 AM
Vishal YelvePosted Jul 14, 2022, 2:41 PM
// Get(response.Content);
public APIResponse GetALMHardwareAPICall(int ID)
{
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Tls13;
var APIEndpoint = _config.GetSection("APIBaseURL").Value + ""
+ _config.GetSection("GetAMLURL").Value
+ "?sysparm_offset=" + ID;
var authorization = _config.GetSection("authorization").Value;
var client = new RestClient(APIEndpoint);
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic " + authorization);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
var resultSet = JsonConvert.DeserializeObject
return resultSet;
}
else
{
return new APIResponse();
}
}
catch (Exception ex)
{
return null;
}
}
POST
public void POSTAsset(APIRequestCreate model)
{
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Tls13;
var _authorization = _config.GetSection("authorization");
var APIEndpoint = _config.GetSection("APIBaseURL") + "" + _config.GetSection("CreateUpdateAsset");
var client = new RestClient(APIEndpoint);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic " + _authorization);
request.AddHeader("content-type", "application/json");
request.AddHeader("Type", "application/x-www-form-urlencoded");
request.AddJsonBody(model, "application/json"); // Post Body
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
var resultSet = response.Content;
}
}
catch (Exception ex)
{
}
}