ASP.NET Core中ocelot.json路由如何传递POST请求的JSON体
Ocelot路由携带JSON请求体操作说明
你给出的Ocelot路由配置的作用是:客户端向Ocelot网关发起POST请求,路径匹配/AddToWishlist时,网关会将请求转发到下游服务地址http://localhost:8003/ProceedToBuy/PostWishList。
Ocelot默认会完整透传请求头、请求体等内容,无需额外修改网关配置,你直接按照普通POST请求携带JSON参数的方式调用上游地址即可,常见调用方式示例如下:
1. curl命令调用
curl -X POST http://<替换为你的Ocelot网关实际地址>/AddToWishlist \ -H "Content-Type: application/json" \ -d '{"userId": 1001, "goodsId": 20001, "goodsNum": 1}'
其中
-d参数后的内容为你实际要传递的JSON业务参数,注意请求头必须携带Content-Type: application/json标识请求体格式。
2. C# HttpClient 调用示例
// 构造业务请求参数 var requestParam = new { UserId = 1001, GoodsId = 20001, GoodsNum = 1 }; // 序列化参数为JSON格式,指定Content-Type var requestContent = new StringContent( JsonSerializer.Serialize(requestParam), Encoding.UTF8, "application/json" ); // 发起请求到Ocelot上游地址 using var client = new HttpClient(); var response = await client.PostAsync("http://<替换为你的Ocelot网关实际地址>/AddToWishlist", requestContent);
3. 前端Axios调用示例
axios.post('http://<替换为你的Ocelot网关实际地址>/AddToWishlist', { userId: 1001, goodsId: 20001, goodsNum: 1 }, { headers: { 'Content-Type': 'application/json' } }).then(res => { console.log('请求结果', res) })
只要网关没有额外配置请求体修改规则,你传入的JSON请求体会原封不动透传给下游的/ProceedToBuy/PostWishList接口。
内容的提问来源于stack exchange,提问作者Aman Gupta
相关产品推荐
相关产品推荐

