首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在restsharp上以multipart/form-data格式发送2个参数?

如何在restsharp上以multipart/form-data格式发送2个参数?
EN

Stack Overflow用户
提问于 2019-04-02 20:57:14
回答 2查看 968关注 0票数 0

我正在使用restsharp。正在尝试发送2个参数,1-yoksis_birim_id (int)

2-资格:(string json)。但是当我尝试的时候,我得到第一个参数是空的响应。这些是我的代码;enter code here

代码语言:javascript
运行
复制
string styleCreateUrl = "http://xxxx/service/qualificationcreate";
var client = new RestClient(styleCreateUrl);
 request.AddHeader("Postman-Token", "xxxx");
 request.AddHeader("cache-control", "no-cache");
 request.AddHeader("Content-Type", "application/json");
 request.AddParameter("Authorization", string.Format("Bearer " + token), ParameterType.HttpHeader);                    
 request.AddParameter("yoksis_birim_id", bolumid, ParameterType.RequestBody);
 request.AddParameter("qualification", bolum, ParameterType.RequestBody);

这是php中的body示例:

代码语言:javascript
运行
复制
   $request->setBody('------WebKitFormBoundary7MA4
Content-Disposition: form-data; name="yoksis_birim_id"

xxxx
------WebKitFormBoundary7MA4
Content-Disposition: form-data; name="qualification"

{"belge_type":"","thematic_code":[],"language":{"tr_TR":{"title":"","nonpreferredterms":"","description":"","further_info":"","further_source":"","url":"","eqflevel":"","nqflevel":"","informationlang":"","sourceofinformation":"","sup_lang":"","sup_url":"","nationaloccupation":"","relation":""},"en_US":{"title":"","nonpreferredterms":"","description":"","further_info":"","further_source":"","url":"","eqflevel":"","nqflevel":"","informationlang":"","sourceofinformation":"","sup_lang":"","sup_url":"","nationaloccupation":"","relation":""}}}
------WebKitFormBoundary7MA4Y');
EN

回答 2

Stack Overflow用户

发布于 2019-04-03 00:04:04

这应该适用于您,您可以通过以下方式向请求体传递多个参数:

代码语言:javascript
运行
复制
request.AddParameter("application/x-www-form-urlencoded", $"yoksis_birim_id={bolumid}&qualification={bolum}", ParameterType.RequestBody);
票数 1
EN

Stack Overflow用户

发布于 2019-04-02 21:03:33

我以JSON的形式发送参数,并传入一个对象类类型,以便将数据转换为字符串。

代码语言:javascript
运行
复制
protected static IRestResponse SendRequest<T>(string baseUrl, string requestUrl, T data)
{
    var restClient = new RestClient(baseUrl);
    var request = new RestRequest(requestUrl, Method.POST);
    request.RequestFormat = DataFormat.Json;

    var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
    request.AddParameter("application/json", json, ParameterType.RequestBody); // <-- Here
    var result = restClient.Execute(request);

    return result;
}

public static IRestResponse SendEmail()
{
    // Call The Function
    var result = SendRequest(baseUrl, urlPageToHit, emailReceiptDto);
    return result;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55475391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档