I have headr parameter and json raw data now I want to check that is working or not. In postman its working proper
Loading
I have headr parameter and json raw data now I want to check that is working or not. In postman its working proper
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 18, 2024, 9:22 AM EST
using HttpClient
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { // Create a HttpClient instance using var httpClient = new HttpClient(); // Set the base address of your API httpClient.BaseAddress = new Uri("https://your-api-base-url.com"); // Add headers as needed httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer your_access_token"); try { // Define your JSON raw data string jsonData = "{\"key\":\"value\"}"; // Create the HTTP request message with POST method and JSON content var request = new HttpRequestMessage(HttpMethod.Post, "/your-api-endpoint"); request.Content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json"); // Send the request and await the response var response = await httpClient.SendAsync(request); // Check if the response is successful if (response.IsSuccessStatusCode) { // Read the response content string responseContent = await response.Content.ReadAsStringAsync(); Console.WriteLine("Response:"); Console.WriteLine(responseContent); } else { // Print error status code and reason phrase Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}"); } } catch (Exception ex) { // Handle any exceptions Console.WriteLine($"Exception: {ex.Message}"); } } }Tuhin PaulRank #7 · 71.9K reputationPosted Feb 18, 2024, 9:23 AM EST
another approach is to use the RestSharp library, which provides a simpler and more intuitive way to make HTTP requests in C#.
using System; using RestSharp; class Program { static void Main(string[] args) { // Define your endpoint URL string apiUrl = "your_api_endpoint_url_here"; // Create RestClient instance RestClient client = new RestClient(apiUrl); // Create RestRequest instance with POST method RestRequest request = new RestRequest(Method.POST); // Set header parameters request.AddHeader("YourHeaderName", "YourHeaderValue"); // Define your JSON data string jsonData = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; // Set request body as JSON data request.AddParameter("application/json", jsonData, ParameterType.RequestBody); // Execute the request IRestResponse response = client.Execute(request); // Check if the request was successful if (response.IsSuccessful) { // Output response content Console.WriteLine("Response:"); Console.WriteLine(response.Content); } else { // Output error message Console.WriteLine("Error: " + response.ErrorMessage); } } }