Replicating this PUT request to RestSharp

Below is an example of a request in a web application.

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 payload field of the request

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

      

Below is my current C # RestSharp code

            // 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 was able to successfully complete all my GET / POST calls, however PUT was not successful.

Capturing Fiddler

Web Application - Work

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 # app - not working

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 />

      

+3


source to share


1 answer


Instead of adding a string as a payload, consider adding an object:



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

      

+1


source







All Articles