Table of Contents

Class RestRequestExtensions

Namespace
RestSharp
Assembly
RestSharp.dll
public static class RestRequestExtensions
Inheritance
RestRequestExtensions
Inherited Members

Methods

AddBody(RestRequest, object, string?)

Adds a body parameter to the request

public static RestRequest AddBody(this RestRequest request, object obj, string? contentType = null)

Parameters

request RestRequest

Request instance

obj object

Object to be used as the request body, or string for plain content

contentType string

Optional: content type

Returns

RestRequest

Remarks

This method will try to figure out the right content type based on the request data format and the provided content type

Exceptions

ArgumentException

Thrown if request body type cannot be resolved

AddFile(RestRequest, string, byte[], string, string?, FileParameterOptions?)

Adds bytes to the request as file attachment

public static RestRequest AddFile(this RestRequest request, string name, byte[] bytes, string filename, string? contentType = null, FileParameterOptions? options = null)

Parameters

request RestRequest

Request instance

name string

Parameter name

bytes byte[]

File content as bytes

filename string

File name

contentType string

Optional: content type. Default is "application/octet-stream"

options FileParameterOptions

File parameter header options

Returns

RestRequest

AddFile(RestRequest, string, Func<Stream>, string, string?, FileParameterOptions?)

Adds a file attachment to the request, where the file content will be retrieved from a given stream

public static RestRequest AddFile(this RestRequest request, string name, Func<Stream> getFile, string fileName, string? contentType = null, FileParameterOptions? options = null)

Parameters

request RestRequest

Request instance

name string

Parameter name

getFile Func<Stream>

Function that returns a stream with the file content

fileName string

File name

contentType string

Optional: content type. Default is "application/octet-stream"

options FileParameterOptions

File parameter header options

Returns

RestRequest

AddFile(RestRequest, string, string, string?, FileParameterOptions?)

Adds a file parameter to the request body. The file will be read from disk as a stream.

public static RestRequest AddFile(this RestRequest request, string name, string path, string? contentType = null, FileParameterOptions? options = null)

Parameters

request RestRequest

Request instance

name string

Parameter name

path string

Full path to the file

contentType string

Optional: content type

options FileParameterOptions

File parameter header options

Returns

RestRequest

AddHeader(RestRequest, string, string)

Adds a header to the request. RestSharp will try to separate request and content headers when calling the resource.

public static RestRequest AddHeader(this RestRequest request, string name, string value)

Parameters

request RestRequest

Request instance

name string

Header name

value string

Header value

Returns

RestRequest

AddHeader<T>(RestRequest, string, T)

Adds a header to the request. RestSharp will try to separate request and content headers when calling the resource.

public static RestRequest AddHeader<T>(this RestRequest request, string name, T value) where T : struct

Parameters

request RestRequest

Request instance

name string

Header name

value T

Header value

Returns

RestRequest

Type Parameters

T

AddHeaders(RestRequest, ICollection<KeyValuePair<string, string>>)

Adds multiple headers to the request, using the key-value pairs provided.

public static RestRequest AddHeaders(this RestRequest request, ICollection<KeyValuePair<string, string>> headers)

Parameters

request RestRequest

Request instance

headers ICollection<KeyValuePair<string, string>>

Collection of key-value pairs, where key will be used as header name, and value as header value

Returns

RestRequest

AddJsonBody<T>(RestRequest, T, string)

Adds a JSON body parameter to the request

public static RestRequest AddJsonBody<T>(this RestRequest request, T obj, string contentType = "application/json") where T : class

Parameters

request RestRequest

Request instance

obj T

Object that will be serialized to JSON

contentType string

Optional: content type. Default is "application/json"

Returns

RestRequest

Type Parameters

T

AddObject<T>(RestRequest, T, params string[])

Gets object properties and adds each property as a form data parameter

public static RestRequest AddObject<T>(this RestRequest request, T obj, params string[] includedProperties) where T : class

Parameters

request RestRequest

Request instance

obj T

Object to add as form data

includedProperties string[]

Properties to include, or nothing to include everything

Returns

RestRequest

Type Parameters

T

AddOrUpdateHeader(RestRequest, string, string)

Adds or updates the request header. RestSharp will try to separate request and content headers when calling the resource. Existing header with the same name will be replaced.

public static RestRequest AddOrUpdateHeader(this RestRequest request, string name, string value)

Parameters

request RestRequest

Request instance

name string

Header name

value string

Header value

Returns

RestRequest

AddOrUpdateHeader<T>(RestRequest, string, T)

Adds or updates the request header. RestSharp will try to separate request and content headers when calling the resource. Existing header with the same name will be replaced.

public static RestRequest AddOrUpdateHeader<T>(this RestRequest request, string name, T value) where T : struct

Parameters

request RestRequest

Request instance

name string

Header name

value T

Header value

Returns

RestRequest

Type Parameters

T

AddOrUpdateHeaders(RestRequest, ICollection<KeyValuePair<string, string>>)

Adds or updates multiple headers to the request, using the key-value pairs provided. Existing headers with the same name will be replaced.

public static RestRequest AddOrUpdateHeaders(this RestRequest request, ICollection<KeyValuePair<string, string>> headers)

Parameters

request RestRequest

Request instance

headers ICollection<KeyValuePair<string, string>>

Collection of key-value pairs, where key will be used as header name, and value as header value

Returns

RestRequest

AddOrUpdateParameter(RestRequest, Parameter)

Adds or updates request parameter, given the parameter instance, for example QueryParameter or UrlSegmentParameter. It will replace an existing parameter with the same name.

public static RestRequest AddOrUpdateParameter(this RestRequest request, Parameter parameter)

Parameters

request RestRequest

Request instance

parameter Parameter

Parameter instance

Returns

RestRequest

AddOrUpdateParameter(RestRequest, string, object, ParameterType, bool)

Adds or updates request parameter of a given type. It will create a typed parameter instance based on the type argument. Parameter will be added or updated based on its name. If the request has a parameter with the same name, it will be updated. It is not recommended to use this overload unless you must, as it doesn't provide any restrictions, and if the name-value-type combination doesn't match, it will throw.

public static RestRequest AddOrUpdateParameter(this RestRequest request, string name, object value, ParameterType type, bool encode = true)

Parameters

request RestRequest

Request instance

name string

Name of the parameter, must be matching a placeholder in the resource URL as {name}

value object

Value of the parameter

type ParameterType

Enum value specifying what kind of parameter is being added

encode bool

Encode the value or not, default true

Returns

RestRequest

AddOrUpdateParameter(RestRequest, string, string?, bool)

Adds or updates a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)

public static RestRequest AddOrUpdateParameter(this RestRequest request, string name, string? value, bool encode = true)

Parameters

request RestRequest

Request instance

name string

Name of the parameter

value string

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

This request

AddOrUpdateParameter<T>(RestRequest, string, T, bool)

Adds or updates a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)

public static RestRequest AddOrUpdateParameter<T>(this RestRequest request, string name, T value, bool encode = true) where T : struct

Parameters

request RestRequest

Request instance

name string

Name of the parameter

value T

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

This request

Type Parameters

T

AddOrUpdateParameters(RestRequest, IEnumerable<Parameter>)

Adds or updates multiple request parameters, given the parameter instance, for example QueryParameter or UrlSegmentParameter. Parameters with the same name will be replaced.

public static RestRequest AddOrUpdateParameters(this RestRequest request, IEnumerable<Parameter> parameters)

Parameters

request RestRequest

Request instance

parameters IEnumerable<Parameter>

Collection of parameter instances

Returns

RestRequest

AddParameter(RestRequest, string?, object, ParameterType, bool)

Adds a parameter of a given type to the request. It will create a typed parameter instance based on the type argument. It is not recommended to use this overload unless you must, as it doesn't provide any restrictions, and if the name-value-type combination doesn't match, it will throw.

public static RestRequest AddParameter(this RestRequest request, string? name, object value, ParameterType type, bool encode = true)

Parameters

request RestRequest

Request instance

name string

Name of the parameter, must be matching a placeholder in the resource URL as {name}

value object

Value of the parameter

type ParameterType

Enum value specifying what kind of parameter is being added

encode bool

Encode the value or not, default true

Returns

RestRequest

AddParameter(RestRequest, string, string?, bool)

Adds a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)

public static RestRequest AddParameter(this RestRequest request, string name, string? value, bool encode = true)

Parameters

request RestRequest
name string

Name of the parameter

value string

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

This request

AddParameter<T>(RestRequest, string, T, bool)

Adds a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)

public static RestRequest AddParameter<T>(this RestRequest request, string name, T value, bool encode = true) where T : struct

Parameters

request RestRequest

Request instance

name string

Name of the parameter

value T

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

This request

Type Parameters

T

AddQueryParameter(RestRequest, string, string?, bool)

Adds a query string parameter to the request. The request resource should not contain any placeholders for this parameter. The parameter will be added to the request URL as a query string using name=value format.

public static RestRequest AddQueryParameter(this RestRequest request, string name, string? value, bool encode = true)

Parameters

request RestRequest

Request instance

name string

Parameter name

value string

Parameter value

encode bool

Encode the value or not, default true

Returns

RestRequest

AddQueryParameter<T>(RestRequest, string, T, bool)

Adds a query string parameter to the request. The request resource should not contain any placeholders for this parameter. The parameter will be added to the request URL as a query string using name=value format.

public static RestRequest AddQueryParameter<T>(this RestRequest request, string name, T value, bool encode = true) where T : struct

Parameters

request RestRequest

Request instance

name string

Parameter name

value T

Parameter value

encode bool

Encode the value or not, default true

Returns

RestRequest

Type Parameters

T

AddStringBody(RestRequest, string, DataFormat)

Adds a string body and figures out the content type from the data format specified. You can, for example, add a JSON string using this method as request body, using DataFormat.Json/>

public static RestRequest AddStringBody(this RestRequest request, string body, DataFormat dataFormat)

Parameters

request RestRequest

Request instance

body string

String body

dataFormat DataFormat

DataFormat for the content

Returns

RestRequest

AddStringBody(RestRequest, string, string)

Adds a string body to the request using the specified content type.

public static RestRequest AddStringBody(this RestRequest request, string body, string contentType)

Parameters

request RestRequest

Request instance

body string

String body

contentType string

Content type of the body

Returns

RestRequest

AddUrlSegment(RestRequest, string, string, bool)

Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work. For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.

public static RestRequest AddUrlSegment(this RestRequest request, string name, string value, bool encode = true)

Parameters

request RestRequest

Request instance

name string

Name of the parameter, must be matching a placeholder in the resource URL as {name}

value string

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

AddUrlSegment<T>(RestRequest, string, T, bool)

Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work. For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.

public static RestRequest AddUrlSegment<T>(this RestRequest request, string name, T value, bool encode = true) where T : struct

Parameters

request RestRequest

Request instance

name string

Name of the parameter, must be matching a placeholder in the resource URL as {name}

value T

Value of the parameter

encode bool

Encode the value or not, default true

Returns

RestRequest

Type Parameters

T

AddXmlBody<T>(RestRequest, T, string, string)

Adds an XML body parameter to the request

public static RestRequest AddXmlBody<T>(this RestRequest request, T obj, string contentType = "application/xml", string xmlNamespace = "") where T : class

Parameters

request RestRequest

Request instance

obj T

Object that will be serialized to XML

contentType string

Optional: content type. Default is "application/xml"

xmlNamespace string

Optional: XML namespace

Returns

RestRequest

Type Parameters

T