Forwarding the entire received request “as is” using HttpClient

落爺英雄遲暮 提交于 2020-03-25 18:21:01

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/60060893/forwarding-the-entire-received-request-as-is-using-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!