tomkuijsten / restup

Webserver for universal windows platform (UWP) apps
MIT License
114 stars 48 forks source link

PostResponse(PostResponse.ResponseStatus.Created) causes empty response error with Angular #83

Closed benbrandt22 closed 7 years ago

benbrandt22 commented 8 years ago

I'm not sure if this would qualify as a bug or an improvement, but I'm running into a scenario with Angular 1.5.7. I'm not familiar with other client side frameworks, so I'm not sure if this is Angular specific or not.

I'm using Angular to post JSON data back to the restup server. This all happens within the context of the client-side app (not a "normal" form post). I want the client side app to react accordingly when the server successfully processes the post. For example, I have a configuration page that has a button that saves data back to the server. Here's a snippet of Javascript from one of my Angular controllers:

configCtrl.saveConfig = function () {
    $http.post("/api/saveConfig", configCtrl.config).then(
        function(response) {
            // success, do something...
        },
        function (error) {
            console.log(error);
        });
};

On the Restup server, I have a Rest controller that receives the posted data and processes it, and returns a success message:

[UriFormat("/saveConfig")]
public IPostResponse SaveConfiguration([FromContent] ConfigViewModel configViewModel)
{
    // process the configViewModel data...
    return new PostResponse(PostResponse.ResponseStatus.Created);
}

In this configuration, Angular posts the data to Restup and it gets processed without issue, but the PostResponse doesn't seem to work. In the client-side browser, Angular interprets the response as an empty response, and handles it as an error. Within the Chrome dev tools, the network tab also shows this Post operation as having an empty response. As a result, the success method on the client is never called.

However, if I use the PostResponse constructor that takes a "locationRedirectUri" parameter, my scenario works, even though I'm not actually redirecting on the client side.

return new PostResponse(PostResponse.ResponseStatus.Created, "index.html");

As a result, I have several C# methods that return PostResponses with redirect URI's, which is a bit misleading to anyone reading the code. Going one step further, I can also use this constructor with an empty string as the redirect and it also works.

return new PostResponse(PostResponse.ResponseStatus.Created, "");

This is a bit better, but doesn't accurately reflect the intention of the code.

tomkuijsten commented 8 years ago

Thanks for the report, sounds like a bug. I struggled a bit to get all the http headers correct for browsers (and fiddler) to accept them as "valid" success responses. I thought that all worked fine now, but appearantly missed this one.