© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
100 replies
Rem

How do I do a HTTP request in C#?

 public static async Task<string> MessageAI(string user, string system)
 {
     HttpClient client = new();
     client.BaseAddress = new("http://localhost:1234");
     client.DefaultRequestHeaders
         .Accept
         .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     var json = @"{
         ""messages"": [
             {
                 ""role"": ""user"",
                 ""content"": user
             },
             {
                 ""role"": ""system"",
                 ""content"": system
             }
         ],
         ""temperature"": 0.7,
         ""max_tokens"": 80,
         ""stream"": false
     }";

     var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
     
     var response = await client.PostAsync("http://localhost:1234/v1/chat/completions", content);
     var responseContent = await response.Content.ReadAsStringAsync();

     return responseContent;
 }
 public static async Task<string> MessageAI(string user, string system)
 {
     HttpClient client = new();
     client.BaseAddress = new("http://localhost:1234");
     client.DefaultRequestHeaders
         .Accept
         .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     var json = @"{
         ""messages"": [
             {
                 ""role"": ""user"",
                 ""content"": user
             },
             {
                 ""role"": ""system"",
                 ""content"": system
             }
         ],
         ""temperature"": 0.7,
         ""max_tokens"": 80,
         ""stream"": false
     }";

     var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
     
     var response = await client.PostAsync("http://localhost:1234/v1/chat/completions", content);
     var responseContent = await response.Content.ReadAsStringAsync();

     return responseContent;
 }

I get bad request?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ c# post request http body
C#CC# / help
3y ago
How do I Retrieve a file in C#
C#CC# / help
11mo ago
✅ How do I program in C#?
C#CC# / help
3y ago
How do i make a c# file in vscode?
C#CC# / help
3y ago