Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a GET request to the path given in the route

Tags:

servicestack

I am trying to call a REST service from a URL like this:

example.org/account/someusername

I have defined request and response DTOs.

[Route("/account/{UserName}", "GET")]
public class AccountRequest : IReturn<AccountResponse>
{
    public string UserName { get; set; }
}

public class AccountResponse
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Bio { get; set; }
}

Calling the service:

JsonServiceClient client = new JsonServiceClient("http://example.org");
AccountRequest request = new AccountRequest { UserName = "me" };

AccountResponse response = client.Get(request); 

However when I call the Get on the client, it doesn't respect the route. When I check the client instance in debugger, AsyncOneWayBaseUri value is example.org/json/asynconeway/. This part is irrelevant because it doesn't mean request is sent to this URL. I actually have no idea where it sends the request. I don't get any errors and all of my properties in response object is null.

What am I missing here?

like image 849
Ufuk Hacıoğulları Avatar asked Mar 26 '13 20:03

Ufuk Hacıoğulları


People also ask

What does a GET request do?

GET is used to retrieve and request data from a specified resource in a server. GET is one of the most popular HTTP request techniques. In simple words, the GET method is used to retrieve whatever information is identified by the Request-URL.

Can we use GET request instead of put to create a resource?

Can I use GET request instead of PUT to create resources? You can, but the only way to pass data in a GET request is by the URL itself.

What is a HTTP route?

A route refers to an HTTP method, path, and handler combination. Routes are created and added to the server before it starts listening for requests.

How do I request a post URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.


1 Answers

Consume 3rd Party REST / HTTP Apis

ServiceStack's Service Clients are opinionated to call ServiceStack web services as they have support for ServiceStack's pre-defined routes, built-in Auth, auto-route generation, built-in Error Handling, etc.

To call 3rd Party REST / HTTP Apis you can use the HTTP Utils that come with ServiceStack.Text, which provide succinct, readable pleasant API's for common data access patterns around .NET's HttpWebRequest, e.g:

List<GithubRepo> repos = "https://api.github.com/users/{0}/repos".Fmt(user)
    .GetJsonFromUrl()
    .FromJson<List<GithubRepo>>();

Consuming ServiceStack services with C# .NET Service Clients

I'm not seeing the reported behavior, are you using the latest version of ServiceStack on the client?

One way to test the generated url that gets used (without making a service call) is to call the TRequest.ToUrl(method) extension method (that the Service Clients uss) directly, e.g.

AccountRequest request = new AccountRequest { UserName = "me" };
request.ToUrl("GET").Print(); //  /account/me

The same auto-generated route was used when I tried calling it via the JsonServiceClient, e.g:

var client = new JsonServiceClient("http://example.org");
var response = client.Get(request); //calls http://example.org/account/me

Route URL used in ServiceStack's Service Clients

ServiceStack will attempt to use the most appropriate route that matches the values populated in the DTO and HTTP Method you're calling with, if there is no matching route it will fallback to the pre-defined routes.

By default the original predefined routes will be used:

/api/[xml|json|html|jsv|csv]/[syncreply|asynconeway]/[servicename]

But ServiceStack now also supports the shorter aliases of /reply and /oneway, e.g:

/api/[xml|json|html|jsv|csv]/[reply|oneway]/[servicename]

Which you can opt-in to use in the clients by setting the flag:

client.UseNewPredefinedRoutes = true;
like image 156
mythz Avatar answered Sep 27 '22 23:09

mythz