简体   繁体   English

在RestSharp中复制此PUT请求

[英]Replicating this PUT request in RestSharp

Below is what the request looks like in the web app. 以下是Web应用程序中的请求。

Request URL:http://myurl.com/rest
Request Method:PUT
Status Code:200 Ok
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:60
Content-Type:application/json
Cookie:ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block
Host:http://myurl
Origin:http://myurl.com
Referer:http://myurl.com/referer
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Request Payload view parsed
{"table":"users","settings":[{"name":"dnd","value":"true"}]}
Response Headersview source
Cache-Control:no-cache
Content-Length:2
Content-Type:application/json

The data is in the request payload field 数据位于请求有效负载字段中

{"table":"users","settings":[{"name":"dnd","value":"true"}]}

Below is my current C# RestSharp code 下面是我目前的C#RestSharp代码

            // Initiate Rest Client
            var client = new RestClient("http://myurl.com");
            var request = new RestRequest("resturl/restrequest");
            // Set headers, method and cookies
            request.Method = Method.PUT;
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "*/*");
            request.AddCookie("session", sessionKey);
            // Set Data format
            request.RequestFormat = DataFormat.Json;
            // Set Data
            string theString = "{'table':'users','settings':[{'name':'dnd','value':'true'}]}";
            request.AddBody(theString);
            // Execute
            var test123 = client.Execute(request);

I've been able to successfully do all my GET/POST calls however PUT has not been successful. 我已经能够成功完成所有的GET / POST呼叫,但是PUT没有成功。

Fiddler Capture 提琴手捕获

Web App - Working 网络应用程序 - 工作

PUT http://myurl HTTP/1.1
Host: http://myurl
Connection: keep-alive
Content-Length: 60
Origin: hhttp://myurl
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://myurl
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block

{"table":"users","settings":[{"name":"dnd","value":"true"}]}

C# Application - Not Working C#应用程序 - 不工作

PUT http://myurl HTTP/1.1
Accept: */*
User-Agent: RestSharp/105.0.1.0
Content-Type: text/xml
Host: http://myurl
Cookie: session=sessionkey
Content-Length: 10
Accept-Encoding: gzip, deflate

<String />

考虑添加对象,而不是添加字符串作为有效负载:

request.AddBody(new { table = "users", settings = new[] { new { name = "dnd", value = "true" } } });

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

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