Как я могу отправить 2 параметра в multipart / form-data на restsharp? - PullRequest
0 голосов
/ 02 апреля 2019

Я использую Restsharp. пытается отправить 2 параметра, 1-yoksis_birim_id (int)

2-квалификация: (строка [json]). Но когда я пытаюсь получить 1-й параметр, это пустой ответ. это мои коды; 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:

   $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');

Ответы [ 2 ]

0 голосов
/ 02 апреля 2019

Это должно работать для вас, вы можете передать несколько параметров в тело запроса:

request.AddParameter("application/x-www-form-urlencoded", $"yoksis_birim_id={bolumid}&qualification={bolum}", ParameterType.RequestBody);
0 голосов
/ 02 апреля 2019

Я отправляю параметры в виде 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;
}
...