سؤال

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.

هل كانت مفيدة؟

المحلول

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");

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top