Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http get to ASP.NET Web Api with object in parameters

I'm trying to get filtered data from server using a filtering object I pass to the server side. I have managed to get this working with a post:

angular:

var filter: { includeDeleted: true, foo: bar };
$http({ method: 'post', url: 'api/stuff', data: filter });

web api:

public IEnumerable<StuffResponse> Post([FromBody]Filter filter)
{
    return GetData(filter);
}

But i don't want to use a post for this, I want to use a get. But this does not work:

angular

$http({ method: 'get', url: 'api/stuff', params: filter });

web api

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

Also tried specifying params: { filter: filter }. If i try [FromBody] or nothing, filter is null. With the FromUri i get an object at least - but with no data. Any ideas how to solve this, without creating input parameters for all filter properties?

like image 689
Trylling Avatar asked Dec 19 '22 17:12

Trylling


1 Answers

Yes you can send data with Get method by sending them with params option

var data ={
  property1:value1,
  property2:value2,
  property3:value3
};

$http({ method: 'GET', url: 'api/controller/method', params: data });

and you will receive this by using [FromUri] in your api controller method

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

and the request url will be like this

http://localhost/api/controller/method?property1=value1&property2=value2&property3=value3
like image 67
Ibrahim Kais Ibrahim Avatar answered Dec 30 '22 17:12

Ibrahim Kais Ibrahim