C
C#2y ago
kevin

❔ 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.'
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
}))
};
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
}))
};
2 Replies
Pobiega
Pobiega2y ago
you don't normally set your content type like that its set on the content itself
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
Encoding.UTF8,
"application/json");//CONTENT-TYPE header
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
Encoding.UTF8,
"application/json");//CONTENT-TYPE header
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?