PageSourceSearch

https://restsharp.dev/assets/js/27330661.c94a73ca.js

js restsharp.dev collected 2026-10-02 10:53:17 UTC 25,882 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs||=[]).push([[9537],{1170(e,t,s){s.r(t),s.d(t,{assets:()=>i,contentTitle:()=>o,default:()=>h,frontMatter:()=>d,metadata:()=>n,toc:()=>l});const n=JSON.parse('{"id":"usage/request","title":"Preparing requests","description":"Create a request","source":"@site/versioned_docs/version-v114/usage/request.md","sourceDirName":"usage","slug":"/usage/request","permalink":"/docs/usage/request","draft":false,"unlisted":false,"editUrl":"https://github.com/RestSharp/RestSharp/tree/dev/docs/versioned_docs/version-v114/usage/request.md","tags":[],"version":"v114","sidebarPosition":4,"frontMatter":{"sidebar_position":4,"title":"Preparing requests"},"sidebar":"tutorialSidebar","previous":{"title":"Creating the client","permalink":"/docs/usage/client"},"next":{"title":"Making calls","permalink":"/docs/usage/execute"}}');var r=s(1987),a=s(7008);const d={sidebar_position:4,title:"Preparing requests"},o=void 0,i={},l=[{value:"Create a request",id:"create-a-request",level:2},{value:"Request headers",id:"request-headers",level:2},{value:"Get or Post parameters",id:"get-or-post-parameters",level:2},{value:"Query string",id:"query-string",level:2},{value:"Using AddObject",id:"using-addobject",level:2},{value:"Using AddObjectStatic",id:"using-addobjectstatic",level:2},{value:"URL segment parameter",id:"url-segment-parameter",level:2},{value:"Cookies",id:"cookies",level:2},{value:"Request Body",id:"request-body",level:2},{value:"String body",id:"string-body",level:3},{value:"JSON body",id:"json-body",level:3},{value:"XML body",id:"xml-body",level:3},{value:"Uploading files",id:"uploading-files",level:2}];function c(e){const t={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",ul:"ul",...(0,a.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.h2,{id:"create-a-request",children:"Create a request"}),"\n",(0,r.jsxs)(t.p,{children:["Before making a request using ",(0,r.jsx)(t.code,{children:"RestClient"}),", you need to create a request instance:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"var request = new RestRequest(resource); // resource is the sub-path of the client base path\n"})}),"\n",(0,r.jsxs)(t.p,{children:["The default request type is ",(0,r.jsx)(t.code,{children:"GET"})," and you can override it by setting the ",(0,r.jsx)(t.code,{children:"Method"})," property. You can also set the method using the constructor overload:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"var request = new RestRequest(resource, Method.Post);\n"})}),"\n",(0,r.jsxs)(t.p,{children:["After you've created a ",(0,r.jsx)(t.code,{children:"RestRequest"}),", you can add parameters to it. Below, you can find all the parameter types supported by RestSharp."]}),"\n",(0,r.jsx)(t.h2,{id:"request-headers",children:"Request headers"}),"\n",(0,r.jsx)(t.p,{children:"Adds the header parameter as an HTTP header that is sent along with the request. The header name is the parameter's name and the header value is the value."}),"\n",(0,r.jsx)(t.p,{children:"You can use one of the following request methods to add a header parameter:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"AddHeader(string name, string value);\nAddHeader<T>(string name, T value);           // value will be converted to string\nAddOrUpdateHeader(string name, string value); // replaces the header if it already exists\n"})}),"\n",(0,r.jsx)(t.p,{children:"For example:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var request = new RestRequest("/path").AddHeader("X-Key", someKey);\n'})}),"\n",(0,r.jsx)(t.p,{children:"You can also add header parameters to the client, and they will be added to every request made by the client. This is useful for adding authentication headers, for example."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"client.AddDefaultHeader(string name, string value);\n"})}),"\n",(0,r.jsx)(t.admonition,{title:"Avoid setting Content-Type header",type:"warning",children:(0,r.jsxs)(t.p,{children:["RestSharp will use the correct content type by default. Avoid adding the ",(0,r.jsx)(t.code,{children:"Content-Type"})," header manually to your requests unless you are absolutely sure it is required. You can add a custom content type to the ",(0,r.jsx)(t.a,{href:"#request-body",children:"body parameter"})," itself."]})}),"\n",(0,r.jsx)(t.h2,{id:"get-or-post-parameters",children:"Get or Post parameters"}),"\n",(0,r.jsxs)(t.p,{children:["The default RestSharp parameter type is ",(0,r.jsx)(t.code,{children:"GetOrPostParameter"}),". You can add ",(0,r.jsx)(t.code,{children:"GetOrPost"})," parameter to the request using the ",(0,r.jsx)(t.code,{children:"AddParameter"})," function:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request\n    .AddParameter("name1", "value1")\n    .AddParameter("name2", "value2");\n'})}),"\n",(0,r.jsxs)(t.p,{children:[(0,r.jsx)(t.code,{children:"GetOrPost"})," behaves differently based on the HTTP method. If you execute a ",(0,r.jsx)(t.code,{children:"GET"})," call, RestSharp will append the parameters to the URL in the form ",(0,r.jsx)(t.code,{children:"url?name1=value1&name2=value2"}),"."]}),"\n",(0,r.jsxs)(t.p,{children:["On a ",(0,r.jsx)(t.code,{children:"POST"})," or ",(0,r.jsx)(t.code,{children:"PUT"})," requests, it depends on whether you have files attached to a request.\nIf not, the parameters will be sent as the body of the request in the form ",(0,r.jsx)(t.code,{children:"name1=value1&name2=value2"}),". Also, the request will be sent as ",(0,r.jsx)(t.code,{children:"application/x-www-form-urlencoded"}),"."]}),"\n",(0,r.jsx)(t.p,{children:"In both cases, name and value will automatically be URL-encoded, unless specified otherwise:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddParameter("name", "V\xe6 \xfc\xe9", false); // don\'t encode the value\n'})}),"\n",(0,r.jsxs)(t.p,{children:["If you have files, RestSharp will send a ",(0,r.jsx)(t.code,{children:"multipart/form-data"})," request. Your parameters will be part of this request in the form:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{children:'Content-Type: text/plain; charset=utf-8\nContent-Disposition: form-data; name="parameterName"\n\nParameterValue\n'})}),"\n",(0,r.jsxs)(t.p,{children:["Sometimes, you need to override the default content type for the parameter when making a multipart form call. It's possible to do by setting the ",(0,r.jsx)(t.code,{children:"ContentType"})," property of the parameter object. As an example, the code below will create a POST parameter with JSON value, and set the appropriate content type:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var parameter = new GetOrPostParameter("someJson", "{\\"attributeFormat\\":\\"pdf\\"}
1") {\n    ContentType = "application/json"\n};\nrequest.AddParameter(parameter);\n'})}),"\n",(0,r.jsx)(t.p,{children:"When the request is set to use multipart content, the parameter will be sent as part of the request with the specified content type:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{children:'Content-Type: application/json; charset=utf-8\nContent-Disposition: form-data; name="someJson"\n\n{"attributeFormat":"pdf"}\n'})}),"\n",(0,r.jsxs)(t.p,{children:["You can also add ",(0,r.jsx)(t.code,{children:"GetOrPost"})," parameter as a default parameter to the client. This will add the parameter to every request made by the client."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'client.AddDefaultParameter("foo", "bar");\n'})}),"\n",(0,r.jsx)(t.p,{children:"It will work the same way as request parameters, except that it will be added to every request."}),"\n",(0,r.jsx)(t.h2,{id:"query-string",children:"Query string"}),"\n",(0,r.jsxs)(t.p,{children:[(0,r.jsx)(t.code,{children:"QueryString"})," works like ",(0,r.jsx)(t.code,{children:"GetOrPost"}),", except that it always appends the parameters to the url in the form ",(0,r.jsx)(t.code,{children:"url?name1=value1&name2=value2"}),", regardless of the request method."]}),"\n",(0,r.jsx)(t.p,{children:"Example:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var client = new RestClient("https://search.me");\nvar request = new RestRequest("search")\n    .AddParameter("foo", "bar");\nvar response = await client.GetAsync<SearchResponse>(request);\n'})}),"\n",(0,r.jsxs)(t.p,{children:["It will send a ",(0,r.jsx)(t.code,{children:"GET"})," request to ",(0,r.jsx)(t.code,{children:"https://search.me/search?foo=bar"}),"."]}),"\n",(0,r.jsxs)(t.p,{children:["For ",(0,r.jsx)(t.code,{children:"POST"}),"-style requests you need to add the query string parameter explicitly:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddQueryParameter("foo", "bar");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["In some cases, you might need to prevent RestSharp from encoding the query string parameter.\nTo do so, set the ",(0,r.jsx)(t.code,{children:"encode"})," argument to ",(0,r.jsx)(t.code,{children:"false"})," when adding the parameter:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddQueryParameter("foo", "bar/fox", false);\n'})}),"\n",(0,r.jsx)(t.p,{children:"You can also add a query string parameter as a default parameter to the client. This will add the parameter to every request made by the client."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'client.AddDefaultQueryParameter("foo", "bar");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["The line above will result in all the requests made by that client instance to have ",(0,r.jsx)(t.code,{children:"foo=bar"})," in the query string for all the requests made by that client."]}),"\n",(0,r.jsx)(t.h2,{id:"using-addobject",children:"Using AddObject"}),"\n",(0,r.jsxs)(t.p,{children:["You can avoid calling ",(0,r.jsx)(t.code,{children:"AddParameter"})," multiple times if you collect all the parameters in an object, and then use ",(0,r.jsx)(t.code,{children:"AddObject"}),".\nFor example, this code:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var params = new {\n    status = 1,\n    priority = "high",\n    ids = new [] { "123", "456" }\n};\nrequest.AddObject(params);\n'})}),"\n",(0,r.jsx)(t.p,{children:"is equivalent to:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddParameter("status", 1);\nrequest.AddParameter("priority", "high");\nrequest.AddParameter("ids", "123,456");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["Remember that ",(0,r.jsx)(t.code,{children:"AddObject"})," only works if your properties have primitive types. It also works with collections of primitive types as shown above."]}),"\n",(0,r.jsxs)(t.p,{children:["If you need to override the property name or format, you can do it using the ",(0,r.jsx)(t.code,{children:"RequestProperty"})," attribute. For example:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'public class RequestModel {\n    // override the name and the format\n    [RequestProperty(Name = "from_date", Format = "d")]\n    public DateTime FromDate { get; set; }\n}\n\n// add it to the request\nrequest.AddObject(new RequestModel { FromDate = DateTime.Now });\n'})}),"\n",(0,r.jsxs)(t.p,{children:["In this case, the request will get a GET or POST parameter named ",(0,r.jsx)(t.code,{children:"from_date"})," and its value would be the current date in short date format."]}),"\n",(0,r.jsx)(t.h2,{id:"using-addobjectstatic",children:"Using AddObjectStatic"}),"\n",(0,r.jsxs)(t.p,{children:["Request function ",(0,r.jsx)(t.code,{children:"AddObjectStatic<T>(...)"})," allows using pre-compiled expressions for getting property values. Compared to ",(0,r.jsx)(t.code,{children:"AddObject"})," that uses reflections for each call, ",(0,r.jsx)(t.code,{children:"AddObjectStatic"})," caches functions to retrieve properties from an object of type ",(0,r.jsx)(t.code,{children:"T"}),", so it works much faster."]}),"\n",(0,r.jsxs)(t.p,{children:["You can instruct ",(0,r.jsx)(t.code,{children:"AddObjectStatic"})," to use custom parameter names and formats, as well as supply the list of properties than need to be used as parameters. The last option could be useful if the type ",(0,r.jsx)(t.code,{children:"T"})," has properties that don't need to be sent with HTTP call."]}),"\n",(0,r.jsxs)(t.p,{children:["To use custom parameter name or format, use the ",(0,r.jsx)(t.code,{children:"RequestProperty"})," attribute. For example:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'class TestObject {\n    [RequestProperty(Name = "some_data")]\n    public string SomeData { get; set; }\n\n    [RequestProperty(Format = "d")]\n    public DateTime SomeDate { get; set; }\n\n    [RequestProperty(Name = "dates", Format = "d")]\n    public DateTime[] DatesArray { get; set; }\n\n    public int        Plain      { get; set; }\n    public DateTime[] PlainArray { get; set; }\n}\n'})}),"\n",(0,r.jsx)(t.h2,{id:"url-segment-parameter",children:"URL segment parameter"}),"\n",(0,r.jsxs)(t.p,{children:["Unlike ",(0,r.jsx)(t.code,{children:"GetOrPost"}),", URL segment parameter replaces placeholder values in the request URL:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var request = new RestRequest("health/{entity}/status")\n    .AddUrlSegment("entity", "s2");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["When the request executes, RestSharp will try to match any ",(0,r.jsx)(t.code,{children:"{placeholder}"})," with a parameter of that name (without the ",(0,r.jsx)(t.code,{children:"{}"}),") and replace it with the value. So the above code results in ",(0,r.jsx)(t.code,{children:"health/s2/status"})," being the URL."]}),"\n",(0,r.jsxs)(t.p,{children:["You can also add ",(0,r.jsx)(t.code,{children:"UrlSegment"})," parameter as a default parameter to the client. This will add the parameter to every request made by the client."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'client.AddDefaultUrlSegment("foo", "bar");\n'})}),"\n",(0,r.jsx)(t.h2,{id:"cookies",children:"Cookies"}),"\n",(0,r.jsxs)(t.p,{children:["You can add cookies to a request using the ",(0,r.jsx)(t.code,{children:"AddCookie"})," method:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddCookie("foo", "bar");\n'})}),"\n",(0,r.jsx)(t.p,{children:"The simple two-parameter form defers domain resolution until execution time \u2014 the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddCookie("foo", "bar", "/path", "example.com");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the ",(0,r.jsx)(t.code,{children:"RestResponse.Cookies"})," properties, which has the ",(0,r.jsx)(t.code,{children:"CookieCollection"})," type."]}),"\n",(0,r.jsxs)(t.p,{children:["There is a ",(0,r.jsx)(t.code,{children:"CookieContainer"})," instance on the request level. You can either assign the pre-populated container to ",(0,r.jsx)(t.code,{children:"request.CookieContainer"}),", or let the container be created automatically at execution time. The four-parameter ",(0,r.jsx)(t.code,{children:"AddCookie"})," overload populates the container immediately, while the two-parameter form stores cookies in ",(0,r.jsx)(t.code,{children:"PendingCookies"})," until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the ",(0,r.jsx)(t.code,{children:"HttpClientHandler"})," level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful."]}),"\n",(0,r.jsxs)(t.p,{children:["If your use case requires sharing cookies between requests made by the client instance, you can use the client-level ",(0,r.jsx)(t.code,{children:"CookieContainer"}),", which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the ",(0,r.jsx)(t.code,{children:"Cookies"})," property of the response and adding them to the client-level container available via ",(0,r.jsx)(t.code,{children:"IRestClient.Options.CookieContainer"})," property."]}),"\n",(0,r.jsx)(t.h2,{id:"request-body",children:"Request Body"}),"\n",(0,r.jsx)(t.p,{children:"RestSharp supports multiple ways to add a request body:"}),"\n",(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.code,{children:"AddJsonBody"})," for JSON payloads"]}),"\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.code,{children:"AddXmlBody"})," for XML payloads"]}),"\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.code,{children:"AddStringBody"})," for pre-serialized payloads"]}),"\n"]}),"\n",(0,r.jsxs)(t.p,{children:["We recommend using ",(0,r.jsx)(t.code,{children:"AddJsonBody"})," or ",(0,r.jsx)(t.code,{children:"AddXmlBody"})," methods instead of ",(0,r.jsx)(t.code,{children:"AddParameter"})," with type ",(0,r.jsx)(t.code,{children:"BodyParameter"}),". Those methods will set the proper request type and do the serialization work for you."]}),"\n",(0,r.jsxs)(t.p,{children:["When you make a ",(0,r.jsx)(t.code,{children:"POST"}),", ",(0,r.jsx)(t.code,{children:"PUT"})," or ",(0,r.jsx)(t.code,{children:"PATCH"})," request and added ",(0,r.jsx)(t.code,{children:"GetOrPost"})," ",(0,r.jsx)(t.a,{href:"#get-or-post-parameters",children:"parameters"}),", RestSharp will send them as a URL-encoded form request body by default. When a request also has files, it will send a ",(0,r.jsx)(t.code,{children:"multipart/form-data"})," request. You can also instruct RestSharp to send the body as ",(0,r.jsx)(t.code,{children:"multipart/form-data"})," by setting the ",(0,r.jsx)(t.code,{children:"AlwaysMultipartFormData"})," property to ",(0,r.jsx)(t.code,{children:"true"}),"."]}),"\n",(0,r.jsxs)(t.p,{children:["You can specify a custom body content type if necessary. The ",(0,r.jsx)(t.code,{children:"contentType"})," argument is available in all the overloads that add a request body."]}),"\n",(0,r.jsx)(t.p,{children:"It is not possible to add client-level default body parameters."}),"\n",(0,r.jsx)(t.h3,{id:"string-body",children:"String body"}),"\n",(0,r.jsxs)(t.p,{children:["If you have a pre-serialized payload like a JSON string, you can use ",(0,r.jsx)(t.code,{children:"AddStringBody"})," to add it as a body parameter. You need to specify the content type, so the remote endpoint knows what to do with the request body. For example:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'const json = "{ data: { foo: \\"bar\\" } }";\nrequest.AddStringBody(json, ContentType.Json);\n'})}),"\n",(0,r.jsx)(t.h3,{id:"json-body",children:"JSON body"}),"\n",(0,r.jsxs)(t.p,{children:["When you call ",(0,r.jsx)(t.code,{children:"AddJsonBody"}),", it does the following for you:"]}),"\n",(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsx)(t.li,{children:"Instructs the RestClient to serialize the object parameter as JSON when making a request"}),"\n",(0,r.jsxs)(t.li,{children:["Sets the content type to ",(0,r.jsx)(t.code,{children:"application/json"})]}),"\n",(0,r.jsxs)(t.li,{children:["Sets the internal data type of the request body to ",(0,r.jsx)(t.code,{children:"DataType.Json"})]}),"\n"]}),"\n",(0,r.jsx)(t.p,{children:"Here is the example:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var param = new MyClass { IntData = 1, StringData = "test123" };\nrequest.AddJsonBody(param);\n'})}),"\n",(0,r.jsxs)(t.p,{children:["It is possible to override the default content type by supplying the ",(0,r.jsx)(t.code,{children:"contentType"})," argument. For example:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'request.AddJsonBody(param, "text/x-json");\n'})}),"\n",(0,r.jsxs)(t.p,{children:["If you use a pre-serialized string with ",(0,r.jsx)(t.code,{children:"AddJsonBody"}),", it will be sent as-is. The ",(0,r.jsx)(t.code,{children:"AddJsonBody"})," will detect if the parameter is a string and will add it as a string body with JSON content type.\nEssentially, it means that top-level str
1ings won't be serialized as JSON when you use ",(0,r.jsx)(t.code,{children:"AddJsonBody"}),". To overcome this issue, you can use an overload of ",(0,r.jsx)(t.code,{children:"AddJsonBody"}),", which allows you to tell RestSharp to serialize the string as JSON:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'const string payload = @"\n""requestBody"": { \n    ""content"": { \n        ""application/json"": { \n            ""schema"": { \n                ""type"": ""string"" \n            } \n        } \n    } \n},";\nrequest.AddJsonBody(payload, forceSerialize: true); // the string will be serialized\nrequest.AddJsonBody(payload); // the string will NOT be serialized and will be sent as-is\n'})}),"\n",(0,r.jsx)(t.h3,{id:"xml-body",children:"XML body"}),"\n",(0,r.jsxs)(t.p,{children:["When you call ",(0,r.jsx)(t.code,{children:"AddXmlBody"}),", it does the following for you:"]}),"\n",(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsx)(t.li,{children:"Instructs the RestClient to serialize the object parameter as XML when making a request"}),"\n",(0,r.jsxs)(t.li,{children:["Sets the content type to ",(0,r.jsx)(t.code,{children:"application/xml"})]}),"\n",(0,r.jsxs)(t.li,{children:["Sets the internal data type of the request body to ",(0,r.jsx)(t.code,{children:"DataType.Xml"})]}),"\n"]}),"\n",(0,r.jsx)(t.admonition,{type:"warning",children:(0,r.jsxs)(t.p,{children:["Do not send XML string to ",(0,r.jsx)(t.code,{children:"AddXmlBody"}),"; it won't work!"]})}),"\n",(0,r.jsx)(t.h2,{id:"uploading-files",children:"Uploading files"}),"\n",(0,r.jsxs)(t.p,{children:["To add a file to the request you can use the ",(0,r.jsx)(t.code,{children:"RestRequest"})," function called ",(0,r.jsx)(t.code,{children:"AddFile"}),". The main function accepts the ",(0,r.jsx)(t.code,{children:"FileParameter"})," argument:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"request.AddFile(fileParameter);\n"})}),"\n",(0,r.jsxs)(t.p,{children:["You can instantiate the file parameter using ",(0,r.jsx)(t.code,{children:"FileParameter.Create"})," that accepts a bytes array, or ",(0,r.jsx)(t.code,{children:"FileParameter.FromFile"}),", which will load the file from disk."]}),"\n",(0,r.jsxs)(t.p,{children:["There are also extension functions that wrap the creation of ",(0,r.jsx)(t.code,{children:"FileParameter"})," inside:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:"// Adds a file from disk\nAddFile(parameterName, filePath, contentType);\n\n// Adds an array of bytes\nAddFile(parameterName, bytes, fileName, contentType);\n\n// Adds a stream returned by the getFile function\nAddFile(parameterName, getFile, fileName, contentType);\n"})}),"\n",(0,r.jsxs)(t.p,{children:["Remember that ",(0,r.jsx)(t.code,{children:"AddFile"})," will set all the necessary headers, so please don't try to set content headers manually."]}),"\n",(0,r.jsxs)(t.p,{children:["You can also provide file upload options to the ",(0,r.jsx)(t.code,{children:"AddFile"})," call. The options are:"]}),"\n",(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.code,{children:"DisableFilenameEncoding"})," (default ",(0,r.jsx)(t.code,{children:"false"}),"): if set to ",(0,r.jsx)(t.code,{children:"true"}),", RestSharp will not encode the file name in the ",(0,r.jsx)(t.code,{children:"Content-Disposition"})," header"]}),"\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.code,{children:"DisableFilenameStar"})," (default ",(0,r.jsx)(t.code,{children:"true"}),"): if set to ",(0,r.jsx)(t.code,{children:"true"}),", RestSharp will not add the ",(0,r.jsx)(t.code,{children:"filename*"})," parameter to the ",(0,r.jsx)(t.code,{children:"Content-Disposition"})," header"]}),"\n"]}),"\n",(0,r.jsx)(t.p,{children:"Example of using the options:"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-csharp",children:'var options = new FileParameterOptions {\n    DisableFilenameEncoding = true,\n    DisableFilenameStar = false\n};\nrequest.AddFile("file", filePath, options: options);\n'})}),"\n",(0,r.jsx)(t.p,{children:"The options specified in the snippet above usually help when you upload files with non-ASCII characters in their names."})]})}function h(e={}){const{wrapper:t}={...(0,a.R)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(c,{...e})}):c(e)}},7008(e,t,s){s.d(t,{R:()=>d,x:()=>o});var n=s(1763);const r={},a=n.createContext(r);function d(e){const t=n.useContext(a);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function o(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:d(e.components),n.createElement(a.Provider,{value:t},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.