I have a thin frontend API web service that does some preprocessing on the received data and then sends the data to my backend API web service using HttpClient.
There are some complex cases when a request contains multipart data with JSON and files, and I don't want to parse it at all in the frontend. The backend will do the job.
So, I would like to take the request "as is" - as raw as possible (not caring about its contents and whether it's multipart or not) and just forward it to the backend API.
I tried the following:
var msg = new HttpRequestMessage(HttpMethod.Post, resourceUrl);
msg.Content = new StreamContent(request.Body);
var apiResponse = await _httpClient.SendAsync(msg);
but the backend web service receives an empty request body with 0 length.
How do I forward the entire request body without having to analyze it and reassemble a new request body?
解决方案
You need to set msg.Content.ContentLength for this to work correctly. It's also a good idea to copy ContentType and other content headers from the request into msg.Content, so that your backend service knows how to parse it.
|