简体   繁体   中英

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. I'm using PostMan for testing route and it works perfectly. but when I'm using RestSharp it doesn't get any content (or data in deserialization case).

Here is my C# code :

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:

function 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM