Question HTTPClient Response Logging

Beanymattd

Member
Joined
Oct 12, 2023
Messages
6
Programming Experience
Beginner
Hi, I have the below code which I am using to send JSON data to an API using HTTPClient. I want to log the HTTP response/status code such as 200, 201, 400 etc. Please can someone assist me with how to log the response code?

C#:
  public void Main()
        {
            //String filePath = @"your-path\test.json";
            String filepath = Dts.Variables["User::FileName"].Value.ToString();
            if (File.Exists(filepath))
            {
                try
                {
                    // Read the JSON file
                    string jsonData = File.ReadAllText(filepath);
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri("https://your-url.com");
                        var response = client.PostAsync("/url-path", new StringContent(jsonData, Encoding.UTF8, "application/json")).Result;
                        var responseContent = response.Content.ReadAsStringAsync();

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error reading or parsing the JSON file: {ex.Message}");
                }
            }
            else
            {
                Console.WriteLine("The JSON file does not exist in the specified folder.");
            }
        }
    }
 

And



Consider using

 

Latest posts

Back
Top Bottom