konedi
konedi
CC#
Created by konedi on 7/29/2024 in #help
Anyone know what is up with HttpClient failing Post request?
closing now. Thank you all.
17 replies
CC#
Created by konedi on 7/29/2024 in #help
Anyone know what is up with HttpClient failing Post request?
It turned out to be the charset=utf-8.... I spent hours on this and did not expect the default content-type application/json that comes with charset=utf-8 to be the issue... sigh I even set the encoding to null on the StringContent object...
17 replies
CC#
Created by konedi on 7/29/2024 in #help
Anyone know what is up with HttpClient failing Post request?
if I had to guess, it's gotta be the Accept-encoding header. I'll check that out.
17 replies
CC#
Created by konedi on 7/29/2024 in #help
Anyone know what is up with HttpClient failing Post request?
Thank you, so it just looks like the HttpClient doesn't have the necessary headers. This is a cloudflare Rest API endpoint btw, so it seems like they are really strict. I just don't understand why HttpClient wouldn't have all the default headers and every other program does(python, js, thunderclient(rest client)).
17 replies
CC#
Created by konedi on 7/29/2024 in #help
Anyone know what is up with HttpClient failing Post request?
error is just a 400 status code and some json schema validation failing on the cloudflare side: {"errors":[{"message":"AiError: Bad input: must be string, must have required property 'image', must be number, must match exactly one schema in oneOf","code":5006}],"success":false,"result":{},"messages":[]} same request works everywhere else. Something with the underlying httpclient request pipeline if I had to guess, but haven't been able to figure it out: Here is the same request with RestSharp:
var inputData = new
{
image = buffer.Select(x => (int)x).ToArray(),
prompt = "this is a video game screenshot, describe the environment and atmosphere",
max_tokens = 256
};

var jsonString = JsonSerializer.Serialize(inputData);

var client = new RestClient();
var request = new RestRequest(url, Method.Post);

request.AddHeader("Authorization", $"Bearer {api_key}");
request.AddHeader("Content-Type", "application/json");
request.AddJsonBody(jsonString);

try
{
var response = await client.ExecuteAsync(request);

if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
}
else
{
Console.WriteLine($"Error: {response.ErrorMessage}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
var inputData = new
{
image = buffer.Select(x => (int)x).ToArray(),
prompt = "this is a video game screenshot, describe the environment and atmosphere",
max_tokens = 256
};

var jsonString = JsonSerializer.Serialize(inputData);

var client = new RestClient();
var request = new RestRequest(url, Method.Post);

request.AddHeader("Authorization", $"Bearer {api_key}");
request.AddHeader("Content-Type", "application/json");
request.AddJsonBody(jsonString);

try
{
var response = await client.ExecuteAsync(request);

if (response.IsSuccessful)
{
Console.WriteLine(response.Content);
}
else
{
Console.WriteLine($"Error: {response.ErrorMessage}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
17 replies