Query string parameters with JSON body?

2,310 views
Skip to first unread message

Josh Earl

unread,
May 3, 2012, 1:17:38 PM5/3/12
to rest...@googlegroups.com
Two APIs I've been working with lately, Remember the Milk and Trello, both expect you to include the API key and user token as query string parameters for POSTs, and then include your data in the request body as JSON.

Unless I'm missing something, RestSharp doesn't seem to support this right now, so I'm having to do something like this:

    var client = new RestClient(baseUrl);
 
    var request = new RestRequest("{version}/cards?key={key}&token={token}"Method.POST) { RequestFormat = DataFormat.Json };
 
    request.AddUrlSegment("version""1")
           .AddUrlSegment("key", key)
           .AddUrlSegment("token", token)
           .AddBody(card);

If I don't do it this way, the key and token get added to the body as post data, wiping out the JSON. My solution seems clumsy, though. 

Would you consider accepting a feature to allow parameters to be explicitly placed in the query string during POST requests, instead of always encoding them in the body? Something like this:

     request.AddUrlSegment("version""1")
           .AddParameter("key", key, ParameterType.QueryString)
           .AddUrlSegment("token", token, ParameterType.QueryString)
           .AddBody(card);
If so, I'll work on a pull request.

Cristovão Morgado

unread,
May 3, 2012, 1:22:11 PM5/3/12
to rest...@googlegroups.com
try this approach

new RestRequest("{version}/cards?key="+ mykey+ "&token="+ mytoken )

request.AddUrlSegment("version""1")
 .AddBody(card);
--
Cristovao Morgado
@TheSaintr


John Sheehan

unread,
May 5, 2012, 1:53:28 AM5/5/12
to rest...@googlegroups.com
The solution you show is the current 'right' way, thought his may
change (see thread on supporting request bodies for DELETE requests,
for which I've proposed a solution that would also address this).

Josh Earl

unread,
May 5, 2012, 3:38:43 PM5/5/12
to rest...@googlegroups.com
The solution you proposed in the DELETE thread would work perfectly for me. RestSharp's defaults are great most of the time, but it would be nice to have a manual override, since we often don't have control of the APIs we're working with. Count me as a vote in favor of this new feature!
Reply all
Reply to author
Forward
0 new messages