簡體   English   中英

RestSharp 沒有獲得響應的數據或內容

[英]RestSharp doesn't get data or content of response

我的 Web 服務中有一條路由,它接收帶有 Json 正文的POST請求並以 Json 格式返回簡單數組。 我正在使用 PostMan 來測試路由,它運行良好。 但是當我使用 RestSharp 時,它沒有得到任何內容(或反序列化情況下的數據)。

這是我的 C# 代碼:

public static async Task<string> UpdateProfile(Profile user, string serviceUrl)
    {

        string bodyraw = JsonConvert.SerializeObject(user)

        var client = new RestClient(serviceUrl);
        var request = new RestRequest();
        request.Method = Method.POST;
        request.Parameters.Clear();
        request.AddParameter("application/json", bodyraw, ParameterType.RequestBody);

        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

        var response = await client.ExecuteTaskAsync<Profile>(request);

        return response.Data.Address;            

    }

這是配置文件類:

 public class Profile
{
    public string Name { get; set; }
    public string Family { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
    public string Postal_code { get; set; }
    public string Education { get; set; }
    public string Gender { get; set; }
    public string Age { get; set; }
    public string Default_contact { get; set; }

    public override string ToString()
    {
        return string.Concat(Name," " ,Family, " ", Address);
    }
}

這是郵差輸出

{
    "Name": "Holma",
    "Family": "Kool",
    "Email": "dr@gmail.com",
    "Mobile": "09063094744",
    "Address": "some city- basic av. sq 60",
    "Postal_code": "10246666",
    "Education": "1",
    "Gender": "male",
    "Age": "35"
}

我使用的 PHP 代碼是:

函數silverum_update_user_profile($request){

$parameters = $request->get_json_params();// this is a WordPress method and works just fine

$name=sanitize_text_field($parameters['name']);
$family=sanitize_text_field($parameters['family']);
$email=sanitize_text_field($parameters['email']);
$mobile=sanitize_text_field($parameters['mobile']);
$address=sanitize_text_field($parameters['address']);
$postal_code=sanitize_text_field($parameters['postal_code']);
$education=sanitize_text_field($parameters['education']);
$gender=sanitize_text_field($parameters['gender']);
$age=sanitize_text_field($parameters['age']);

$extdp = [
    "Name"=>$name,
    "Family"=>$family,
    "Email"=>$email,
    "Mobile"=>$mobile,
    "Address"=>$address,
    "Postal_code"=>$postal_code,
    "Education"=>$education,
    "Gender"=>$gender,
    "Age"=>$age
];


return $extdp;

}

當 PHP 方法返回“參數”時,它可以,PostMan 和 RestSharp 都可以看到輸出內容,但是當方法返回新數組時,只有 PostMan 能夠接收返回的對象。 我在這個問題上花了幾個小時,但一無所獲。 請幫忙。

嘗試在 RestRequest 對象中使用 AddJsonBody() 方法,而不是手動添加參數。

public static async Task<string> UpdateProfile(Profile user, string serviceUrl)
{
    var client = new RestClient(serviceUrl);
    var request = new RestRequest();
    request.Method = Method.POST;
    request.AddJsonBody(user);

    request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

    var response = await client.ExecuteAsync<Profile>(request);

    return response.Data.Address;            
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM