Итак, я нашел этот пример, созданный с помощью RestSharp:
var client = new RestClient("api.com/api/");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddHeader("authorization", $"Bearer {access_token}");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"sku\"\r\n\r\n{\"merchantId\": 6663, \"externalCode\" : \"90131\", \"status\" : \"AVAILABLE\", \"name\" : \"Name 1\", \"description\": \"Test\", \"schedules\" : [], \"availability\": \"AVAILABLE\", \"price\" : {\"originalValue\": 0.0,\"promotional\": false,\"value\" : 0.00}}\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful) return true;
return false;
Приведенный выше код работает, но я пытаюсь сделать то же самое, используя HttpClient. Вот моя попытка:
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri))
{
string bodyRequest = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"sku9\"\r\n\r\n{\"merchantId\": 6663, \"externalCode\" : \"1251\", \"status\" : \"AVAILABLE\", \"name\" : \"Name 1\", \"description\": \"Test\", \"schedules\" : [], \"availability\": \"AVAILABLE\", \"price\" : {\"originalValue\": 0.0,\"promotional\": false,\"value\" : 0.00}}\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n-----011000010111000001101001--\r\n";
request.Content = new StringContent(bodyRequest);
request.Content.Headers.Clear();
request.Content.Headers.Add("ContentType", "multipart/form-data, boundary=---011000010111000001101001");
using (HttpResponseMessage response = await ApiClient.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
Console.WriteLine(response.ReasonPhrase);
return false;
}
}
}
Я получаю эту ошибку:
{
"timestamp": "2020-03-02T10:03:42.724+0000",
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.multipart.MultipartException",
"message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is javax.servlet.ServletException: Content-Type != multipart/form-data",
"path": "/skus"
}