0

Let's say I have an entity called "Entity" that has three properties - "Id", "Name" and "Number".

Using AngularJS, how can I use $http.get to send a GET request for only the objects with a specific "Number"? For example, out of all the objects in that entity, I only want to GET the objects with the number 20. How can I do that?

Thank you!

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
Telmo F.
  • 167
  • 1
  • 4
  • 16

2 Answers2

1

It depends your API but you could do something like

     $http({
         method  : 'GET',
         url     : 'your_api_path/object/20',
         headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
     .success(function(data) {
         $scope.data = data
      });

or

$http({
    url: 'your_api_path', 
    method: "GET",
    params: {object_id: object.id}
 });
Dan M. CISSOKHO
  • 1,070
  • 1
  • 12
  • 27
1

You must pass an criteria on which your API will return the corresponding value.

You can have an class

[Route("api/[controller]")]
public class EntityController : Controller
{

   [HttpGet("{number}")]
   public Entity Get(int number)
   {
       return yourList.Where(en => en.Number == number).FirstOrDefault();
   }
}

and an $http request

$http.get('yourHost://api/entity/20')
     .success(function(data) {
         $scope.data = data
      });

or

$http({
    url: 'yourHost://api/entity', 
    method: "GET",
    params: {number: yourNumber}
 });
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • I'm having trouble understanding the changes you want me to make on my Visual Studio controller. My controller is the default one, as I didn't make any changes to it. Do I put your code in the `GET: api/Entity` section? Can you also explain your variables a bit more, like the `yourList` and the `number`? Is that number the actual number I want to filter from the "Number" property, i.e. 20? – Telmo F. Aug 11 '16 at 09:38
  • @T.Ferreira don't change your controller, only adjust it. I only show you an example. `number` is the parameter you pass, `yourList` is the list of items from which you want to get the values – Suren Srapyan Aug 11 '16 at 09:38
  • @T.Ferreira I only say that you need to pass the number on which you want to filter to the API method, and your API must return the properly value – Suren Srapyan Aug 11 '16 at 09:40
  • Please check this pastebin, it is the part of my controller that relates to GET: http://pastebin.com/gNYEjtd5. `ITApps_KBTM_Execution` is the name of the entity. What changes should I make here in order to filter the parameter "Number"? – Telmo F. Aug 11 '16 at 09:44
  • @T.Ferreira create an method which will take an number an your list will find which one's number is equal to the passed number and return that item – Suren Srapyan Aug 11 '16 at 09:52
  • And I should create this method where? In the Visual Studio controller? Or in my Javascript file? – Telmo F. Aug 11 '16 at 09:55
  • Do you have maybe a quick example on how to do such a method? I'm really not that great in C#. – Telmo F. Aug 11 '16 at 10:01