3

I am currently developing a Windows Phone 8 app, consuming REST API and I'm using the well known library "RESTSharp" to request the API services.

My problem is that the REST API is exposing a method that requires to use the HTTP LINK verb and I was unable to find anything related to use this verb with RESTSharp (even more with the so generic term "link").

So if someone could give me some advice or just point me in the right direction, that would be very kind of you.

Feel free to ask for further details.

Community
  • 1
  • 1
Invvard
  • 1,497
  • 2
  • 15
  • 20

2 Answers2

4

Originally, I had thought that there was not a way to do this using RestSharp, since the IRestRequest interface uses a Method enum to represent the HTTP method. However, it appears that RestSharp does, in fact, support using non-standard HTTP verbs via the ExecuteAsGet<T> and ExecuteAsPost<T> methods on the RestClient class. Which method you use depends on the semantics of the non-standard HTTP method. If the request has a body (or "entity" in HTTP parlance) then you would use ExecuteAsPost; if it does not, then you would use ExecuteAsGet.

Here is some sample code demonstrating how to use RestSharp to do a LINK request, based on one of the examples in the draft memo referenced in your question.

RestClient client = new RestClient("http://example.org");
RestRequest request = new RestRequest("/images/my_dog.jpg");
request.AddHeader("Link", "<http://example.org/profiles/joe>; rel=\"tag\"");
IRestResponse response = client.ExecuteAsGet(request, "LINK");
Debug.WriteLine((int)response.StatusCode + " " + response.StatusDescription);

UPDATE

It seems you are right, the WP8 project does not include RestClient.Sync.cs, which contains the synchronous ExecuteAsGet<T> and ExecuteAsPost<T> methods. However there are asynchronous versions, ExecuteAsyncGet<T> and ExecuteAsyncPost<T> which should be available to you in WP8. Both of these accept a string httpMethod parameter as well.

client.ExecuteAsyncGet(request, (response, handle) =>
    {
        Debug.WriteLine((int)response.StatusCode + " " + response.StatusDescription);
    }, 
    "LINK");
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Right now, the ExecuteAsGet() is not supported on the RestSharp API for WP8 apps. There's a HttpFactory that seems to exists into the RestClient class. There's no doc to read but I'll look into it. – Invvard Nov 03 '13 at 22:26
  • 1
    There are async version of these methods that should be available to you in WP8. I've updated my answer. – Brian Rogers Nov 04 '13 at 02:17
  • Thank you for those details, "ExecuteAsyncGet" is a winner to execute a "LINK" request ! – Invvard Nov 04 '13 at 22:53
1

Support for non-standard methods was added a while back. You can do:

restClient.ExecuteAsPost(restRequest, "LINK") 

or use ExecuteAsGet. Basically, you use one or the other based on whether you pass query string or request body.

I personally feel this is an awful API. The new HttpClient has better API for non-standard methods.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • @Invvard - I was talking of the RestSharp API. And sorry, didn't really concentrate on the WP8 tag. – manojlds Nov 04 '13 at 06:22