Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38508

Sending a GET request to the path given in the route

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?

Upvotes: 5

Views: 6774

Answers (4)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38508

Thanks everyone for their answers. C# client was sending the request to the right address from the start, I debugged it with Fiddler. Only I wasn't deserializing it properly.

Account object was in the data property of the response, not the response itself. The client is good at working with REST services even if they are not built with ServiceStack. It is pretty cool.

Upvotes: 0

mythz
mythz

Reputation: 143399

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;

Upvotes: 8

mickfold
mickfold

Reputation: 2003

Servicestack supports a number of different data formats, such as JSON, XML, JSV, CSV, etc. and supports a number of different endpoints for accessing this data out of the box. Please find below details of the supported endpoints that has been taken from the formats section of the SS documentation.

The clients provided by ServiceStack use the default endpoint, not the restful endpoint to access the data. The data is still accessible restfully, you can test this by navigating to the restful URL in your browser.

Restful Endpoints

You can define which format should be used by adding ?format={format} to the end of the URL.

  • ?format=json
  • ?format=xml
  • ?format=jsv
  • ?format=csv
  • ?format=htm

Example: http://www.servicestack.net/ServiceStack.Hello/servicestack/hello/World!?format=json

Alternatively ServiceStack also recognizes which format should be used with the Accept http header:

  • Accept: application/json
  • Accept: application/xml

As you can see, this approach only works with json and xml.

Default endpoint

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

Examples:

  • /servicestack/xml/[syncreply|asynconeway]/[servicename] will be XML
  • /servicestack/json/[syncreply|asynconeway]/[servicename] will be JSON

SOAP endpoint

The SOAP endpoint only supports XML of course.


UPDATE

The ServiceStack clients cannot be used to connect to a non-ServiceStack web service because they rely on behavior which is specific to ServiceStack. Its probably best to use something like RestSharp or one of the many other available clients that allow you to interact with a restful web service.

Upvotes: 1

paaschpa
paaschpa

Reputation: 4816

it doesn't respect the route

Are you getting a 404 or a Handler not found exception?

Make sure whatever assembly your 'AccountService' class is in is added to the 'assembliesWithServices' parameter when configuring your AppHost. It sounds like the your Route is not being picked up by ServiceStack.

public MyAppHost() : base("my app", typeof(AccountService).Assembly) { }  

What does your Service class look like?

Something like below should work (don't forget the Service interface)

public class AccountService : Service
{
    public object Any(AccountRequest request)
    {
        return new AccountResponse() { UserName = request.UserName};
    }
}

Upvotes: 1

Related Questions