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
requestRestRequestRequest instance
objobjectObject to be used as the request body, or string for plain content
contentTypestringOptional: 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
requestRestRequestRequest instance
namestringParameter name
bytesbyte[]File content as bytes
filenamestringFile name
contentTypestringOptional: content type. Default is "application/octet-stream"
optionsFileParameterOptionsFile 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
requestRestRequestRequest instance
namestringParameter name
getFileFunc<Stream>Function that returns a stream with the file content
fileNamestringFile name
contentTypestringOptional: content type. Default is "application/octet-stream"
optionsFileParameterOptionsFile 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
requestRestRequestRequest instance
namestringParameter name
pathstringFull path to the file
contentTypestringOptional: content type
optionsFileParameterOptionsFile 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
requestRestRequestRequest instance
namestringHeader name
valuestringHeader 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
requestRestRequestRequest instance
namestringHeader name
valueTHeader 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
requestRestRequestRequest instance
headersICollection<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
requestRestRequestRequest instance
objTObject that will be serialized to JSON
contentTypestringOptional: 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
requestRestRequestRequest instance
objTObject to add as form data
includedPropertiesstring[]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
requestRestRequestRequest instance
namestringHeader name
valuestringHeader 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
requestRestRequestRequest instance
namestringHeader name
valueTHeader 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
requestRestRequestRequest instance
headersICollection<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
requestRestRequestRequest instance
parameterParameterParameter 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
requestRestRequestRequest instance
namestringName of the parameter, must be matching a placeholder in the resource URL as {name}
valueobjectValue of the parameter
typeParameterTypeEnum value specifying what kind of parameter is being added
encodeboolEncode 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
requestRestRequestRequest instance
namestringName of the parameter
valuestringValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
namestringName of the parameter
valueTValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
parametersIEnumerable<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
requestRestRequestRequest instance
namestringName of the parameter, must be matching a placeholder in the resource URL as {name}
valueobjectValue of the parameter
typeParameterTypeEnum value specifying what kind of parameter is being added
encodeboolEncode 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
requestRestRequestnamestringName of the parameter
valuestringValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
namestringName of the parameter
valueTValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
namestringParameter name
valuestringParameter value
encodeboolEncode 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
requestRestRequestRequest instance
namestringParameter name
valueTParameter value
encodeboolEncode 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
requestRestRequestRequest instance
bodystringString body
dataFormatDataFormatDataFormat 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
requestRestRequestRequest instance
bodystringString body
contentTypestringContent 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
requestRestRequestRequest instance
namestringName of the parameter, must be matching a placeholder in the resource URL as {name}
valuestringValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
namestringName of the parameter, must be matching a placeholder in the resource URL as {name}
valueTValue of the parameter
encodeboolEncode 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
requestRestRequestRequest instance
objTObject that will be serialized to XML
contentTypestringOptional: content type. Default is "application/xml"
xmlNamespacestringOptional: XML namespace
Returns
Type Parameters
T