Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

Asp.Net Web API to get getails of a particular item

Hi every one I am new to Asp.net-web-api and I want to get details of a particular item where the item_Code has to be passed in the header of postman or in json format.

I did as follows

public List<Product> GetByCode(string ItemCode)
{
    using (var context = new DBContext())
    {
        var getItem = (from s in context.objProduct where (s.ItemCode == ItemCode) select s).ToList();
        if (getItem!=null )
        {
            return getItem;
        }
        else
        {
            return null;
        }
    }
}

If I am doing this through query string like

localhost:50787/API/Product/GetByCode?ItemCod=3F-47-AB-84-9F-EB-D6-6B-9C-62-CC-85-98-4D-28-6B

as GET its working fine. But I want it has to be passed through JSON or Header with key and values.

Please help me.

Upvotes: 1

Views: 841

Answers (2)

Nkosi
Nkosi

Reputation: 247153

First you will need to post the request to your end point. To do that you will need to edit your action with [HttpPost] attribute.

[HttpPost]
public List<Product> GetByCode(string ItemCode) {...}

I would also suggest changing the name of the action as posting to GetByCode can cause some confusion with its name.

With that done, lets focus now on how to send data to the endpoint.

Sending a post to the action like

POST localhost:50787/API/Product/GetByCode?ItemCod=3F-47-AB-84-9F-EB-D6-6B-9C-62-CC-85-98-4D-28-6B HTTP/1.1
User-Agent: Fiddler
Host: localhost:50787

will work, but you state

But I want it has to be passed through JSON or Header with key and values.

Passing it through the header will require a lot more work on your part.

Take this request for example where the code is sent in a custom header.

POST localhost:50787/API/Product/GetByCode HTTP/1.1
User-Agent: Fiddler
Host: localhost:50787
ItemCode: 3F-47-AB-84-9F-EB-D6-6B-9C-62-CC-85-98-4D-28-6B

You would have to customize the API to look for that specific header key and then map it to the intended action parameter. More work than you really need to do.

To send it as JSON body and have it still call your action with a simple type, you will need to update your action to know how to deal with the request.

[HttpPost]
public List<Product> GetByCode([FromBody]string ItemCode) {...}

Take note of the [FromBody] attribute.

Using [FromBody]

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

In the above example, Web API will use a media-type formatter to read the value of ItemCode from the request body. Here is an example client request.

POST localhost:50787/API/Product/GetByCode HTTP/1.1    
User-Agent: Fiddler
Host: localhost:50787
Content-Type: application/json
Content-Length: 49

"3F-47-AB-84-9F-EB-D6-6B-9C-62-CC-85-98-4D-28-6B"

Source: Parameter Binding in ASP.NET Web API

Upvotes: 1

Luca Ghersi
Luca Ghersi

Reputation: 3321

You can do something like this.

Create a class for your model:

public class Item
{
    public string Code {get;set;}
}

and change your controller like this:

[HttpPost]
public List<Product> GetByCode([FromBody]Item item)
{
    using (var context = new DBContext())
    {
        var getItem = (from s in context.objProduct where (s.ItemCode == item.Code) select s).ToList();
        if (getItem!=null )
        {
            return getItem;
        }
        else
        {
            return null;
        }
    }
}

Hope it helps :)

Upvotes: 1

Related Questions