简体   繁体   English

RestSharp 没有获得响应的数据或内容

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

I have a route in my web service that receives POST request with Json body and returns simple array in Json format.我的 Web 服务中有一条路由,它接收带有 Json 正文的POST请求并以 Json 格式返回简单数组。 I'm using PostMan for testing route and it works perfectly.我正在使用 PostMan 来测试路由,它运行良好。 but when I'm using RestSharp it doesn't get any content (or data in deserialization case).但是当我使用 RestSharp 时,它没有得到任何内容(或反序列化情况下的数据)。

Here is my C# code :这是我的 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;            

    }

And here is the Profile Class:这是配置文件类:

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

And this is 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"
}

And the PHP code that I used is:我使用的 PHP 代码是:

function silverum_update_user_profile($request ){函数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;

} }

When PHP method returns "Parameter" its OK and both PostMan and RestSharp can see output content but when method Returns new Array only PostMan is able to recive returnd object.当 PHP 方法返回“参数”时,它可以,PostMan 和 RestSharp 都可以看到输出内容,但是当方法返回新数组时,只有 PostMan 能够接收返回的对象。 I spent a couple of hour on the issue but didn't get anywhere.我在这个问题上花了几个小时,但一无所获。 help please.请帮忙。

Try using the AddJsonBody() method within the RestRequest object as opposed to adding the parameter manually.尝试在 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