简体   繁体   English

RestSharp PUT请求参数

[英]RestSharp PUT request parameters

I'm having issues figuring out how to create a put request using RestSharp. 我在弄清楚如何使用RestSharp创建放置请求时遇到问题。

I need to pass an integer followed by a JSON body in the same request. 我需要在同一请求中传递一个整数,后跟一个JSON主体。

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. 我已经在POST上测试了JSON广告requestBody,并且效果很好。 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. 我认为我的问题与我尝试传递ReorderedTasks[i].ID的第一个参数有关,我不确定我是否正确处理了此传递。

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. 我建议将ReorderedTasks [i] .ID.ToString()直接放在url路径中。

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

It will help to reduce possible problems with http request format. 这将有助于减少http请求格式的可能问题。

Well it depends on what does the webApi expect.. 好吧,这取决于webApi的期望。

You could use Fiddler to inspect what being sent through the wire and what response You are getting ( http://www.telerik.com/fiddler ) 您可以使用Fiddler检查通过电线发送的内容以及得到的响应( http://www.telerik.com/fiddler

Also - here are some sample's how other users use RestSharp How do I use PUT in RestSharp? 另外-这是一些示例,其他用户如何使用RestSharp 我如何在RestSharp中使用PUT?

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) 如果您的端点URL具有要与请求一起传递的?param=value&param2=value类的?param=value&param2=value ,则RestSharp的AddParameter(string, string)不能与PUT方法一起使用(但它与GET或端点没有URL参数,因此具有欺骗性)

Use AddParameter(string, string, ParameterType.QueryString) in order to PUT Method work correctly. 使用AddParameter(string, string, ParameterType.QueryString)才能使PUT Method正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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