Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for sending query string parameters AND a request body. #237

Closed
wants to merge 4 commits into from

3 participants

@jeff-french

Our API requires a query string parameter with each call. When this parameter is specified for PUT or POST requests RestSharp ignores the request body added with AddBody(). This is resolved by adding an AlwaysUseQueryString property to the RestRequest and Http classes.

@johnsheehan
Owner

This is sort of an edge case and can be handled by moving the querystring parameters into the method URL and setting their values with URL segments:

request = new RestRequest("foo?bar={bar}&fizz={fizz}");
request.AddUrlSegment("bar", 123);

I don't believe this use case justifies a change in RestRequest. Happy to discuss it more on the Google Group

@johnsheehan johnsheehan closed this
@jeff-french

Looks like I really over thought that one! I remember reading that the URL segment parameters used simple string replacement, just didn't think about applying it to the query string. Thanks for the reminder to keep it simple, John. :)

@xetorthio

This is actually a pain point in RestSharp.
Sending query string parameters when POSTing is important.
Using AddUrlSegment will not always help, as sometimes (like in my case) query string parameters are sent by the user, I don't know them in advance.
Is there any reason why no to add support for this?
Probably request.AddQueryStringParameter or something similar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 RestSharp/Http.cs
@@ -46,6 +46,13 @@ public IHttp Create()
return new Http();
}
+ /// <summary>
+ /// Set to true to send HTTP parameters in the query string
+ /// for POST and PUT requests. This allows a request body to
+ /// be sent in addition to the query string parameters.
+ /// </summary>
+ public bool AlwaysUseQueryString { get; set; }
+
/// <summary>
/// True if this HTTP request has any HTTP parameters
/// </summary>
@@ -277,7 +284,7 @@ private void PreparePostBody(HttpWebRequest webRequest)
{
webRequest.ContentType = GetMultipartFormContentType();
}
- else if(HasParameters)
+ else if(!AlwaysUseQueryString && HasParameters)
{
webRequest.ContentType = "application/x-www-form-urlencoded";
RequestBody = EncodeParameters();
View
1  RestSharp/IHttp.cs
@@ -28,6 +28,7 @@ public interface IHttp
ICredentials Credentials { get; set; }
string UserAgent { get; set; }
int Timeout { get; set; }
+ bool AlwaysUseQueryString { get; set; }
#if !SILVERLIGHT
bool FollowRedirects { get; set; }
#endif
View
6 RestSharp/IRestRequest.cs
@@ -225,5 +225,11 @@ public interface IRestRequest
Action<IRestResponse> OnBeforeDeserialization { get; set; }
void IncreaseNumAttempts();
+
+ /// <summary>
+ /// When set to True, GetOrPost parameters will always
+ /// be sent as a query string regardless of the Http Method.
+ /// </summary>
+ bool AlwaysUseQueryString { get; set; }
}
}
View
3  RestSharp/RestClient.cs
@@ -316,7 +316,7 @@ public Uri BuildUri(IRestRequest request)
}
}
- if (request.Method != Method.POST && request.Method != Method.PUT && request.Method != Method.PATCH)
+ if ((request.AlwaysUseQueryString) || (request.Method != Method.POST && request.Method != Method.PUT && request.Method != Method.PATCH))
{
// build and attach querystring if this is a get-style request
if (request.Parameters.Any(p => p.Type == ParameterType.GetOrPost))
@@ -350,6 +350,7 @@ private string EncodeParameters(IRestRequest request)
private void ConfigureHttp(IRestRequest request, IHttp http)
{
http.CookieContainer = CookieContainer;
+ http.AlwaysUseQueryString = request.AlwaysUseQueryString;
// move RestClient.DefaultParameters into Request.Parameters
foreach(var p in DefaultParameters)
View
10 RestSharp/RestRequest.cs
@@ -50,7 +50,7 @@ public RestRequest()
Files = new List<FileParameter>();
XmlSerializer = new XmlSerializer();
JsonSerializer = new JsonSerializer();
-
+ AlwaysUseQueryString = false;
OnBeforeDeserialization = r => { };
}
@@ -453,7 +453,13 @@ public void IncreaseNumAttempts()
_attempts++;
}
- /// <summary>
+ /// <summary>
+ /// When set to True, GetOrPost parameters will always
+ /// be sent as a query string regardless of the Http Method.
+ /// </summary>
+ public bool AlwaysUseQueryString { get; set; }
+
+ /// <summary>
/// How many attempts were made to send this Request?
/// </summary>
/// <remarks>
View
4 restsharp.nuspec
@@ -2,7 +2,7 @@
<package>
<metadata>
<id>RestSharp</id>
- <version>102.7</version>
+ <version>102.9</version>
<authors>John Sheehan, RestSharp Community</authors>
<owners>John Sheehan</owners>
<description>Simple REST and HTTP API Client</description>
@@ -15,6 +15,8 @@
</dependencies>
<tags>REST HTTP API JSON XML</tags>
<releaseNotes>
+ 102.9 - Added aupport for sending query string parameters AND request body in POST and PUT requests.
+ 102.8 - Added support for sending query string paramters with all Http Methods.
102.7 - Updating Json.NET to 4.0.8, misc fixes
102.6 - Updating Json.NET reference to 4.0.5
</releaseNotes>
Something went wrong with that request. Please try again.