2

I am using AngularJs and Resources module. I want to do a GET to obtain an object.. to do this GET I do not have to pass simply the ID to the server, but I should pass a complex object with different properties and values..

Here the code I am using:

$scope.getActivationStatus = function (event) {
    event.preventDefault();

    if ($scope.segui_attivazione_form.$valid) {
        $scope.activationStatus =
            new SeguiAttivazioneService
                .seguiAttivazione()
                .$get(
                    {
                        request: $scope.activationStatus
                    }, function () { });
    }
};

On server side I have:

[HttpGet]
public IHttpActionResult GetActivationStatus(MyComplexObject request)
{
    //I will do something here later...
    return Ok();
}

The problem is that "request" arrive on server equals to NULL... I have solved the problem passing two strings to the server... in this way:

$scope.getActivationStatus = function (event) {
    event.preventDefault();

    if ($scope.segui_attivazione_form.$valid) {
        $scope.activationStatus =
            new SeguiAttivazioneService
                .seguiAttivazione()
                .$get(
                    {
                        codiceFiscale: $scope.activationStatus.CodiceFiscale,
                        codiceRichiesta: $scope.activationStatus.CodiceRichiesta
                    }, function () { });
    }
};

And server side:

[HttpGet]
public IHttpActionResult GetActivationStatus(string codiceFiscale, string codiceRichiesta)
{
    return Ok();
}

In this way everything works... but I don't like this solution because I will have more than two input... And this is a get, not a post (not a save, an update)...

How can I pass a complex object doing a GET?

Thank you...

Simone
  • 2,304
  • 6
  • 30
  • 79

4 Answers4

4

It's best to use the POST method if you want to send data in the body of the request. While it's possible with Angular, some servers might ignore the body of GET requests.

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • 1
    But if I want to use resources of angular a POST is equivalent to $save... but I do not to have to do a save... but a get... https://docs.angularjs.org/api/ngResource/service/$resource see under the RETURNS subtitle – Simone May 03 '15 at 12:42
  • @Ciccio can create a custom action in the resource as explained in docs – charlietfl May 03 '15 at 12:45
  • 1
    It's not even very "`RESTful`" to have to use complex objects in `GET` requests. If it's just a few parameters, you could pass them in the path. – P Varga May 03 '15 at 12:45
  • ok Peter, so you mean that If I have a form to make a search with a lot of filters, I should you a list of string. Right? It could make sense I am just "worried" for the number of filters... – Simone May 03 '15 at 12:56
  • charlie, no... I can't.. everytime I try (It is not the first service I do) something does not work... I don't know why.. almost surely, mine is not the right way to use Angularjs, reosurces and services... – Simone May 03 '15 at 12:58
  • 1
    How long URLs can be depends on the browser, but modern browsers allow very long ones. If you keep them under `2000` characters, you should be fine. – P Varga May 03 '15 at 13:01
3

This approach allows to send complex objects with arrays and sub objects:

Angular:

$http({
       url: '/myApiUrl',
       method: 'GET',
       params: { param1: angular.toJson(myComplexObject, false) }
      })

C#:

[HttpGet]
public string Get(string param1)
{
     Type1 obj = new JavaScriptSerializer().Deserialize<Type1>(param1);
     ...
}
Vildan
  • 1,934
  • 1
  • 16
  • 17
0

This is not an elegant solution but it works using HTTP GET:

$http.get(url + "?" + $.param(obj).replace(/%5b([^0-9].*?)%5d/gi, '.$1'))

It converts the complex object into a string with dot notation to define levels. Most of the server side frameworks like ASP.NET Core can bind it to complex objects.

This is an example of the string I send for a complex object:

StartDate=2021-06-11&EndDate=2021-06-11&TimeRange.TimeFrom.Time=07%3A00&TimeRange.TimeFrom.TimeFrame=AM&TimeRange.TimeTo.Time=10%3A00&TimeRange.TimeTo.TimeFrame=AM

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
-1

Request body can only be sent by POST. With get you could at best URL Encode the OBJECT and then send it as query string params. But thats not the best solution to post some data to the server