C#
C#

help

Root Question Message

>_<
>_<3/11/2023
❔ Misused header name

Hi, I get the following error and I'm not sure on how to fix it.

System.InvalidOperationException: 'Misused header name, 'content-type'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'


Heres my code:

            using var httpClient = new HttpClient();
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri($"https://api.openai.com/v1/{_model}/completions"),
                Headers =
            {
                { "Content-Type", "application/json" },
                { "Authorization", $"Bearer {_apiKey}" },
            },
                Content = new StringContent(JsonSerializer.Serialize(new
                {
                    prompt = question,
                    max_tokens = 50,
                    temperature = 0.7
                }))
            };
Pobiega
Pobiega3/11/2023
you don't normally set your content type like that
Pobiega
Pobiega3/11/2023
its set on the content itself
Pobiega
Pobiega3/11/2023
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
                                    Encoding.UTF8, 
                                    "application/json");//CONTENT-TYPE header
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy