I’m trying to write a Post request in the custom function. In php can be written using CURL, What about dotnet? What’s the best way to convert the below code to .Net?
$header = array(
'Authorization: Basic '. base64_encode(Key).’:’)
);
$curl = curl_init(‘URL’);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
$response = json_decode(curl_exec($curl), true);
I didn't have any experience in .net. I hope find the answer
Find a solution of send request use .net 5.0
.Net 5 is for web application and we have official document here.
Firstly, we need to inject http service into our project, which is the first step in the document doing. Adding
services.AddHttpClient();in theStartup.cs/ConfigureServicesmethod which is used to injectIHttpClientFactory, so that we could use this http module in our main method.Then in your method, you need to inject
IHttpClientFactoryinto the constructor method.