API Request Issue

Pixilated

New member
Joined
Feb 13, 2025
Messages
3
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:
What problem did you encounter incorporating @JohnH 's suggestion? It doesn't look like you incorporated it at all.

What errors are you getting?
What response are you getting from the request?
 
Hello ,
i cannot run the code because of the following issues in code:


1739455580155.png


Severity Code Description Project File Line Suppression state
Error CS0029 The type “string” cannot be implicitly converted to “byte[]”.

Severity Code Description Project File Line Suppression state
Error CS0029 The type “string” cannot be implicitly converted to “System.DateTimeOffset?”.

Severity Code Description Project File Line Suppression state
Error CS0029 The type “string” cannot be implicitly converted to “System.Net.Http.Headers.AuthenticationHeaderValue”.


Thank you for any help !
 

Attachments

  • 2025-02-13 15_03_44-Phoneword - Microsoft Visual Studio.png
    2025-02-13 15_03_44-Phoneword - Microsoft Visual Studio.png
    65.4 KB · Views: 0
@JohnH explicitly told you about those type issues. His code was meant to point you to the direction of setting the headers via the properties rather than your approaching of adding custom headers. You were not meant to just copy and paste code. You were meant to read and understand what was being communicated.
 
Back
Top Bottom