API Request Issue

Pixilated

New member
Joined
Feb 13, 2025
Messages
2
Programming Experience
3-5
Dear all ,
I despair of the following API call:

My Code:

C#:
 using (HttpClient client = new HttpClient())
 {
     try
     {
         var request = new HttpRequestMessage(HttpMethod.Post, url)
         {
            Content = new StringContent(body, Encoding.UTF8, contentType)
         };
       

         request.Headers.Add("Content-MD5", contentMD5);
         request.Headers.Add("Date", date);
         request.Headers.Add("Authorization", authorization);


         HttpResponseMessage response = await client.SendAsync(request);
         response.EnsureSuccessStatusCode(); 

         string responseBody = await response.Content.ReadAsStringAsync();
         Console.WriteLine(responseBody);
     }

Result :
Error: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.


Thank you for any help !
 
Last edited by a moderator:
Try setting the header properties instead:
C#:
var content = new StringContent(body, Encoding.UTF8, contentType);
content.Headers.ContentMD5 = contentMD5;

var request = new HttpRequestMessage(HttpMethod.Post, url)
{
    Content = content
};
request.Headers.Date = date;
request.Headers.Authorization = authorization;
The properties are strongly typed, ContentMD5 is type byte[] which is the hash before converting to base64.
 
Dear John ,

thank you very much for your answer!
unfortunately I still can't get any further..
I m not able to add your code successfully so far.

Here is my complete call.

Thanks for any further help


C#:
 private async void MakeApiRequest()
 {


     string keyId = "xxxxxxx";
     string secret = "xxxxxxxxx";
     string url = "https://www.soliscloud.com:13333/v1/api/inverterDetailList";
     string body = "{\"sn\":\"010F92235310037\"}";

     string contentMD5;
     using (var md5 = MD5.Create())
     {
         byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
         contentMD5 = Convert.ToBase64String(md5.ComputeHash(bodyBytes));
     }

     string contentType = "application/json";
     string date = DateTime.UtcNow.ToString("R");  // RFC1123-Format
     Console.WriteLine(date);

     string encryptStr = $"POST\n{contentMD5}\n{contentType}\n{date}\n/v1/api/inverterDetailList";
     Console.WriteLine(encryptStr);

   
     string sign;
     using (var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(secret)))
     {
         byte[] signatureBytes = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(encryptStr));
         sign = Convert.ToBase64String(signatureBytes);
     }

     string authorization = $"API {keyId}:{sign}";

 
     using (HttpClient client = new HttpClient())
     {
         try
         {
             var request = new HttpRequestMessage(HttpMethod.Post, url)
             {
                Content = new StringContent(body, Encoding.UTF8, contentType)
             };

             request.Headers.Add("Content-MD5", contentMD5);
             request.Headers.Add("Date", date);
             request.Headers.Add("Authorization", authorization);

             HttpResponseMessage response = await client.SendAsync(request);
             response.EnsureSuccessStatusCode();

             string responseBody = await response.Content.ReadAsStringAsync();
             Console.WriteLine(responseBody);
         }
         catch (Exception ex)
         {
             Console.WriteLine($"Error: {ex.Message}");
         }
 
Last edited by a moderator:
Back
Top Bottom