[英]How to upload an XML file using multipart/form-data with Restsharp?
[英]How can i send 2 parameters in multipart/form-data at restsharp?
我正在使用restsharp。 試圖發送2個參數,1-yoksis_birim_id(int)
2資格:(字符串[json])。 但當我嘗試我得到第一參數是空響應。 這些是我的代碼; enter code here
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樣本:
$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');
我發送參數作為JSON並傳入[data]的對象類類型以轉換為字符串。
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;
}
這應該適合您,您可以通過以下方式將多個參數傳遞給請求正文:
request.AddParameter("application/x-www-form-urlencoded", $"yoksis_birim_id={bolumid}&qualification={bolum}", ParameterType.RequestBody);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.