简体   繁体   中英

RestSharp PUT request parameters

I'm having issues figuring out how to create a put request using RestSharp.

I need to pass an integer followed by a JSON body in the same request.

So far I have this:

for (var i = 0; i < ReorderedTasks.Count; i++) {
            var reorderedTasksJson = new JavaScriptSerializer().Serialize(ReorderedTasks[i]);
            var request = new RestRequest("api/task/5/{ID}/", Method.PUT);
            request.AddParameter("ID", ReorderedTasks[i].ID.ToString(), ParameterType.UrlSegment);
            request.AddParameter("application/json; charset=utf-8", reorderedTasksJson, ParameterType.RequestBody);
            client.Execute(request);
        }

I've tested out the JSON ad requestBody on POST and it works fine. I think my issue is with the first parameter I'm trying to pass ReorderedTasks[i].ID , I'm not sure if I'm handling the passing of this correctly.

I've initialised client at the beginning of my class.

Problem is the DB isn't updating and I need to isolate the problem. Is the above the correct way in dealing with my two parameters needing passed?

I suggest to put ReorderedTasks[i].ID.ToString() directly to url path.

var request = new RestRequest($"api/task/5/{ReorderedTasks[i].ID.ToString()}/", Method.PUT);

It will help to reduce possible problems with http request format.

Well it depends on what does the webApi expect..

You could use Fiddler to inspect what being sent through the wire and what response You are getting ( http://www.telerik.com/fiddler )

Also - here are some sample's how other users use RestSharp How do I use PUT in RestSharp?

I'll add it here, so someone will benefit from it.

If your endpoint URL have parameters like ?param=value&param2=value that you want to pass along with request RestSharp's AddParameter(string, string) won't work with PUT method (but it works just fine with GET or if endpoint doesn't have URL parameters, so it is deceiving)

Use AddParameter(string, string, ParameterType.QueryString) in order to PUT Method work correctly.

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