ошибка 400, глагол запроса недействителен (только формы Xamarin)
Запуск тех же значений в Insomnia и возврат ОК 200, но когда я делаю это в Xamarin, это дает мне ошибку 400, что я делаю не так?
Это значения, которые отправляются в Inspection Data (Body multipart)
- id_extintor = 34
- mes = 202409
- tipo_inspeccion = W
- boquilla = OK
- мангера = ОК
- presion = NR
- песо = 13 кг
- Sello = ОК
- seguro = ОК
- сенализация = ОК
- delimitado = ОК
- observaciones = ТЕСТОВЫЙ ПОСТ
- evidencia = [изображение, прикрепленное к устройству, преобразованное в байт[]]
- creado_por = user
creado_por = user
public ApiService()
{
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
Credentials = new NetworkCredential("USRB", "PSWRB")
};
_httpClient = new HttpClient(handler);
_httpClient.BaseAddress = new Uri("http://10.10.4.32/api/");
}
public async Task InsertExtInspect(InspectionData inspectionData)
{
try
{
if (Application.Current.Properties.ContainsKey("UserID"))
{
inspectionData.CREADO_POR = Application.Current.Properties["UserID"] as string;
}
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent(inspectionData.ID_EXTINTOR.ToString()), name: "id_extintor");
formData.Add(new StringContent(inspectionData.MES), name: "mes");
formData.Add(new StringContent(inspectionData.TIPO_INSPECCION), name: "tipo_inspeccion");
formData.Add(new StringContent(inspectionData.BOQUILLA), name: "boquilla");
formData.Add(new StringContent(inspectionData.MANGUERA), name: "manguera");
formData.Add(new StringContent(inspectionData.PRESION), name: "presion");
formData.Add(new StringContent(inspectionData.PESO), name: "peso");
formData.Add(new StringContent(inspectionData.SELLO), name: "sello");
formData.Add(new StringContent(inspectionData.SEGURO), name: "seguro");
formData.Add(new StringContent(inspectionData.SENALIZACION), name: "senalizacion");
formData.Add(new StringContent(inspectionData.DELIMITADO), name: "delimitado");
formData.Add(new StringContent(inspectionData.OBSERVACIONES), name: "observaciones");
formData.Add(new StringContent(inspectionData.CREADO_POR), name: "creado_por");
if (inspectionData.EVIDENCIA != null)
{
if (inspectionData.EVIDENCIA.Length > 0)
{
var fileContent = new ByteArrayContent(inspectionData.EVIDENCIA);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "evidencia",
FileName = "evidencia.jpg"
};
formData.Add(fileContent);
}
else
{
// Evidenve empty
var emptyContent = new ByteArrayContent(new byte[0]);
emptyContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
emptyContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "evidencia",
FileName = "evidencia.jpg"
};
formData.Add(emptyContent);
}
}
else
{
// IF Evidence is null
var emptyContent = new ByteArrayContent(new byte[0]);
emptyContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
emptyContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "evidencia",
FileName = "evidencia.jpg"
};
formData.Add(emptyContent);
}
var requestUri = "Inspeccion";
var response = await _httpClient.PostAsync(requestUri, formData);
var statusCode = response.StatusCode;
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response status code: {statusCode}");
Console.WriteLine($"Response content: {responseContent}");
return response.IsSuccessStatusCode;
}
}
catch (HttpRequestException httpRequestException)
{
Console.WriteLine($"Request error: {httpRequestException.Message}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"General error: {ex.Message}");
return false;
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... tpclient-c