POST to ServiceStack Service and get the location header

I am trying to send a POST to my ServiceStack service and get the Location header from the response of my CREATED object. I'm not sure if using IReturn is valid, but I'm not sure how to access the response headers from my client. Can someone help me understand how to interact with HttpResult correctly? There is a test case at the bottom of the code to demonstrate what I want to do. Here's the codz:

public class ServiceStackSpike { public class AppHost : AppHostHttpListenerBase { public AppHost() : base("TODOs Tests", typeof(Todo).Assembly) { } public override void Configure(Container container) { //noop } } [Route("/todos", "POST")] public class Todo:IReturn<HttpResult> { public long Id { get; set; } public string Content { get; set; } public int Order { get; set; } public bool Done { get; set; } } public class TodosService : Service { public object Post(Todo todo) { //do stuff here var result = new HttpResult(todo,HttpStatusCode.Created); result.Headers[HttpHeaders.Location] = "/tada"; return result; } } public class NewApiTodosTests : IDisposable { const string BaseUri = "http://localhost:82/"; AppHost appHost; public NewApiTodosTests() { appHost = new AppHost(); appHost.Init(); appHost.Start(BaseUri); } [Fact] public void Run() { var restClient = new JsonServiceClient(BaseUri); var todo = restClient.Post(new Todo { Content = "New TODO", Order = 1 }); Assert.Equal(todo.Headers[HttpHeaders.Location], "/tada"); //=>fail } public void Dispose() { appHost.Dispose(); appHost = null; } } } 
+7
source share
1 answer

See Configuring HTTP Responses ServiceStack widget page for all the different ways to configure an HTTP response.

A HttpResult is just one way to set up an HTTP response. Usually you want to enable Absolute Url if you are going to redirect it, for example:

 public object Post(Todo todo) { var todo = ...; return new HttpResult(todo, HttpStatusCode.Created) { Location = base.Request.AbsoluteUri.CombineWith("/tada") }; } 

Note. HTTP clients will never see the HttpResult DTO. HttpResult is not the DTO itself , it is only for capturing and modifying the configured HTTP response you want.

All ServiceStack clients return - this is the HTTP body, which in this case is the Todo Response DTO. The location is indeed added to the headers of the HTTP response, and to see the entire response of the HTTP response, you should use an HTTP sniffer like Fiddler, WireShark or Chrome WebInspector.

If you want to access it using ServiceStack HTTP clients, you will need to add a response filter that will give you access to HttpWebResponse , for example:

 restClient.ResponseFilter = httpRes => { Assert.Equal(httpRes.Headers[HttpHeaders.Location], "/tada"); }; Todo todo = restClient.Post(new Todo { Content = "New TODO", Order = 1 }); 

Checking response headers using web request extensions

Another lightweight alternative if you just want to check the HTTP response is to use the ServiceStack Convenient WebRequest extension methods, for example:

 var url = "http://path/to/service"; var json = url.GetJsonFromUrl(httpRes => { Assert.Equal(httpRes.Headers[HttpHeaders.Location], "/tada"); }); 
+7
source

All Articles