xeon48 xeon48 - 1 year ago 104
reST (reStructuredText) Question

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

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
, 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 there 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?

Answer Source

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.