You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RestSharp 111.3版本如何修改表单数据内的Content-Type值?

在RestSharp 111.3中修改Form Data内部的Content-Type

要解决multipart/form-data中JSON参数被标记为text/plain的问题,你可以用RestSharp的AddPart方法替代AddParameter,它允许你显式指定参数的Content-Type:

方法1:直接传入JSON字符串

var request = new RestRequest(options);
// 第三个参数指定Content-Type为application/json
request.AddPart("ARequest", "{\"attributeFormat\":\"pdf\"}", "application/json");

方法2:序列化对象后传入

如果是从对象生成JSON,先序列化再传入更规范:

var request = new RestRequest(options);
// 构造数据对象
var attributeData = new { attributeFormat = "pdf" };
// 序列化为JSON字符串(可使用Newtonsoft.Json或System.Text.Json)
var jsonContent = System.Text.Json.JsonSerializer.Serialize(attributeData);
// 添加到表单并指定Content-Type
request.AddPart("ARequest", jsonContent, "application/json");

这样设置后,Fiddler捕获的请求中对应表单项的Content-Type会变为application/json; charset=utf-8,符合需求。

原来的AddParameter方法对于字符串类型的表单参数,默认会自动设置Content-Type为text/plain,而AddPart方法支持自定义该属性,完美适配这种场景。

内容的提问来源于stack exchange,提问作者Jeff R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:32:11