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
RestRequestRequest instance
obj
objectObject to be used as the request body, or string for plain content
contentType
stringOptional: content type
Returns
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
RestRequestRequest instance
name
stringParameter name
bytes
byte[]File content as bytes
filename
stringFile name
contentType
stringOptional: content type. Default is "application/octet-stream"
options
FileParameterOptionsFile parameter header options
Returns
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
RestRequestRequest instance
name
stringParameter name
getFile
Func<Stream>Function that returns a stream with the file content
fileName
stringFile name
contentType
stringOptional: content type. Default is "application/octet-stream"
options
FileParameterOptionsFile parameter header options
Returns
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
RestRequestRequest instance
name
stringParameter name
path
stringFull path to the file
contentType
stringOptional: content type
options
FileParameterOptionsFile parameter header options
Returns
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
RestRequestRequest instance
name
stringHeader name
value
stringHeader value
Returns
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
RestRequestRequest instance
name
stringHeader name
value
THeader value
Returns
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
RestRequestRequest 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
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
RestRequestRequest instance
obj
TObject that will be serialized to JSON
contentType
stringOptional: content type. Default is "application/json"
Returns
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
RestRequestRequest instance
obj
TObject to add as form data
includedProperties
string[]Properties to include, or nothing to include everything
Returns
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
RestRequestRequest instance
name
stringHeader name
value
stringHeader value
Returns
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
RestRequestRequest instance
name
stringHeader name
value
THeader value
Returns
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
RestRequestRequest 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
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
RestRequestRequest instance
parameter
ParameterParameter instance
Returns
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
RestRequestRequest instance
name
stringName of the parameter, must be matching a placeholder in the resource URL as {name}
value
objectValue of the parameter
type
ParameterTypeEnum value specifying what kind of parameter is being added
encode
boolEncode the value or not, default true
Returns
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
RestRequestRequest instance
name
stringName of the parameter
value
stringValue of the parameter
encode
boolEncode 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
RestRequestRequest instance
name
stringName of the parameter
value
TValue of the parameter
encode
boolEncode 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
RestRequestRequest instance
parameters
IEnumerable<Parameter>Collection of parameter instances
Returns
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
RestRequestRequest instance
name
stringName of the parameter, must be matching a placeholder in the resource URL as {name}
value
objectValue of the parameter
type
ParameterTypeEnum value specifying what kind of parameter is being added
encode
boolEncode the value or not, default true
Returns
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
RestRequestname
stringName of the parameter
value
stringValue of the parameter
encode
boolEncode 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
RestRequestRequest instance
name
stringName of the parameter
value
TValue of the parameter
encode
boolEncode 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
RestRequestRequest instance
name
stringParameter name
value
stringParameter value
encode
boolEncode the value or not, default true
Returns
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
RestRequestRequest instance
name
stringParameter name
value
TParameter value
encode
boolEncode the value or not, default true
Returns
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
RestRequestRequest instance
body
stringString body
dataFormat
DataFormatDataFormat for the content
Returns
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
RestRequestRequest instance
body
stringString body
contentType
stringContent type of the body
Returns
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
RestRequestRequest instance
name
stringName of the parameter, must be matching a placeholder in the resource URL as {name}
value
stringValue of the parameter
encode
boolEncode the value or not, default true
Returns
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
RestRequestRequest instance
name
stringName of the parameter, must be matching a placeholder in the resource URL as {name}
value
TValue of the parameter
encode
boolEncode the value or not, default true
Returns
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
RestRequestRequest instance
obj
TObject that will be serialized to XML
contentType
stringOptional: content type. Default is "application/xml"
xmlNamespace
stringOptional: XML namespace
Returns
Type Parameters
T