'Bad request by accent in the value of a property of the payload c# httpClient
I am calling an api that uploads documents to an X repository using HttpClient.
I am emulating the call just like I do in postman using FORM-DATA. The problem is that I am serializing a request, that request has a property called filename and when the value of that property has a special character or an accent, the call immediately returns a Bad Request (400) but when that property goes without an accent, the service responds well OK (200)... what can I do?
public async Task Send(Book request)
{
using (var client = new HttpClient())
{
string apiUrl = GetUrl();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, apiUrl);
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(request.documentBase64));
var payload = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var requestContent = new MultipartFormDataContent()
{
{
new StreamContent(stream), "documentFormData", request.filename
},
{
payload, "payloadRequest"
}
};
req.Content = requestContent;
using (HttpResponseMessage response = await client.SendAsync(req))
{
//if request.filename = 'imagen_acción.png' gets BAD REQUEST(400)
// but if request.filename = 'imagen_accion.png' gets OK(200)
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
