httpcline转发_在“原样”转发整个接收到的请求。使用HttpClient

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:02   5338   0

bd96500e110b49cbb3cd949968f18be7.png

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.

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP