RestSharp无法获取数据或响应内容

时间:2020-02-11 16:29:32

标签: c# php json postman restsharp

我的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);
    }
}

这是 PostMan OutPut:

{
    "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方法返回“ Parameter”时,它的状态为OK,并且PostMan和RestSharp都可以看到输出内容,但是当方法返回new Array时,只有PostMan可以接收返回的对象。我花了几个小时解决这个问题,但一无所获。请帮助。

1 个答案:

答案 0 :(得分:0)

尝试在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;            
}
相关问题