konedi
Anyone know what is up with HttpClient failing Post request?
I am making a post request to a cloudflare ai worker and HttpClient can't seem to succeed. I tried other rest clients, javascript,python and including RestSharp and it works.
Here is the code:
byte[] buffer = await File.ReadAllBytesAsync(image_path);
// Create the input data object
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
};
// Serialize the input data to JSON
string jsonString = JsonSerializer.Serialize(inputData);
// Create an HttpClient instance
using (HttpClient httpClient = new HttpClient())
{
// Set up the request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_key);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
// Prepare the content for the request
var content = new StringContent(jsonString, null, "application/json");
try
{
// Send the POST request
HttpResponseMessage response = await httpClient.PostAsync(url, content);
// Check the response
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
byte[] buffer = await File.ReadAllBytesAsync(image_path);
// Create the input data object
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
};
// Serialize the input data to JSON
string jsonString = JsonSerializer.Serialize(inputData);
// Create an HttpClient instance
using (HttpClient httpClient = new HttpClient())
{
// Set up the request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_key);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
// Prepare the content for the request
var content = new StringContent(jsonString, null, "application/json");
try
{
// Send the POST request
HttpResponseMessage response = await httpClient.PostAsync(url, content);
// Check the response
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
17 replies