Greg Yolshin
Greg Yolshin

Reputation: 13

WebApi 2 + Owin Use urlencoded body on token request

In my WebAPI2 app I use OAuth authentication through Owin middleware. In order to get token client should use application/x-www-form-urlencoded body in request.

 function userAccount($resource, appSettings) {
    return {
        registration: $resource(appSettings.serverPath + "/api/Account/Register", null, 
                {
                    'registerUser' : { method : 'POST'}
                }
            ),
        login : $resource(appSettings.serverPath + "/Token", null, 
                {
                    'loginUser': {
                        method: 'POST',
                        headers: {
                            'Content-Type' : 'application/x-www-form-urlencoded' 
                        },
                        transformRequest: function (data, headersGetter) {
                            var str = [];
                            for (var d in data) {
                                str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                            }
                            return str.join("&"); 
                        }
                    }
                } 
            )
    }
}

But is there any method to override this behaviour to use raw body in json format? Instead of this: "grant_type=password&username=user&password=123456" want to use this: "{ grant_type: "password", username:"user", password="123456" }".

Appreciate any suggests.

Upvotes: 1

Views: 793

Answers (1)

sonicblis
sonicblis

Reputation: 3194

You could set up an action in a controller as a "proxy" method that could accept the json in the body and then call the internal method with the url encoded parameters.

Upvotes: 1

Related Questions