Sending a GET request to the path specified on the route

I am trying to call a REST service from a URL, for example:

example.org/account/someusername

I have defined a DTO request and response.

[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; }
}

Service Call:

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

AccountResponse response = client.Get(request); 

However, when I call Get on the client, it does not respect the route. When I test the client instance in the debugger, the value of AsyncOneWayBaseUri is equal to example.org/json/asynconeway/ . This part does not matter, because it does not mean that the request is sent to this URL. In fact, I have no idea where he is sending the request. I get no errors and all my properties in the response object are null.

What am I missing here?

+5
source share
4 answers

. # , Fiddler. .

Account data , . REST, ServiceStack. .

0

REST/HTTP Apis

ServiceStack Service , - ServiceStack, ServiceStack, Auth, , ..

REST/HTTP Apis HTTP Utils, ServiceStack.Text, , API .NET HttpWebRequest, :

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

ServiceStack .NET #

, ServiceStack ?

URL-, ( ), , TRequest.ToUrl(method) ( ),

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

, JsonServiceClient, :

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

URL- , ServiceStack

ServiceStack , , DTO HTTP-, , , .

:

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

ServiceStack /reply /oneway, :

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

, :

client.UseNewPredefinedRoutes = true;
+8

Servicestack , JSON, XML, JSV, CSV .. . , formats SS.

, ServiceStack, , . - , , URL- .

, , ?format={format} URL-.

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

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

ServiceStack , Accept http:

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

, json xml.

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

:

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

SOAP

SOAP XML, .


UPDATE

ServiceStack -, ServiceStack, , ServiceStack. , - RestSharp , -.

+1

404 ?

Make sure that the entire assembly that your AccountService class is in is added to the 'assemblysiesWithServices' parameter when setting up your AppHost. It seems that your route has not been 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};
    }
}
+1
source

All Articles