Showing posts with label Postman. Show all posts
Showing posts with label Postman. Show all posts

Wednesday, May 20, 2020

Step by step how to add custom headers using Asp.Net Core 3.0

In this tutorial we'll learn   how to add custom headers using Asp.Net Core 3.0 
We'll use Postman for testing purposes of a RESTful WEBAPI Core application, sending  HTTP POST requests. We'll start with a WebApi Core 3.0 application,  and we'll add an Extension Method to the Response class in order to append our headers.
See the code here:
https://github.com/CarmelSchvartzman/DOTNET/blob/master/HEADERS/ADD_HEADERS_EXTENSIONMETHOD





Step by step how to add custom headers using Asp.Net Core 3.0



First, we create a new Extension Method to the Response class in order to append our headers., in Visual Studio, as follows:





  public static class Extensions
    {
        public static void AddApplicationError(this HttpResponse response, string msg)
        {
            response.Headers.Add("Carmel-Error", msg);
            response.Headers.Add("Access-Control-Expose-Headers", "Carmel-Error");
            response.Headers.Add("Access-Control-Allow-Origin", "*");
        }

    }

On the startup.cs file , we add to  the Configure method , the following code:

  if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(b => {
                    b.Run(async ctx => {
                        ctx.Response.StatusCode = (int) 
HttpStatusCode.InternalServerError;
                        var err = ctx.Features.Get<IExceptionHandlerFeature>();
                        if (err != null)
                        {
                            ctx.Response.AddApplicationError(err.Error.Message);
                            await  ctx.Response.WriteAsync(err.Error.Message);
                        }
                         
                    });
                });
            }

This code catchs all exceptions globally, and also adds our headers to the response.
Send a request using Postman, and open the "headers" window to see them:












That's all.... 
In this tutorial we've seen  how to add custom headers using Asp.Net Core 3.0  
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן











Tuesday, January 5, 2016

How to send an HTTP DELETE Request to a RESTful ODataController Web API Service using Postman

In this post we'll learn Step by step how to send an HTTP DELETE Request to a RESTful ODataController Web API Service using Postman
We'll use Postman to test a RESTful OData Web API application, sending an HTTP DELETE request. We'll use a working OData Web API ODataController  built in a previous tutorial,   and we'll delete a record using  Postman :

How to send an HTTP DELETE Request to a RESTful ODataController Web API Service using Postman




In order to get Postman installed ,  go to the Chrome Tools >> Extensions   ,  search for "Postman" and install the App .

HTTP DELETE Request to a RESTful ODataController Web API with Postman



Now let's see how to call the OData Web API to delete an item :   in order to setup  the ODataController , there will be an ODataModelBuilder at the "Register" method called from the Global.asax file : it's important that the EntitySet name MUST be the same name of the Controller   , therefore we'll look for a "NotesController"  at the application :

Because there is a route prefix set ( "ODataV4" ) ,  we'll also append it to the URL : 



At the ODataController ,  we check for the "Delete" action method , since we're sending an HTTP DELETE request :


Why are we checking this ? Because we want to know what is ODataController method expecting : in our case , it expects a URI which must include an integer "key" ID  ( "[FromODataURI]" ) .  

The Port of the application can be extracted from the Web tab at the application properties:


Put all of this together , and you have the URI for the DELETE request : 

"http://localhost:21435/ODataV4/Notes(6)" :


Important : OData is case sensitive : therefore , if you type "notes" instead of "Notes" , you will not obtain any data.

After setting the URI and the DELETE method , write the "Content-Type" header :




Send the request :


The request details can be seen this way :


In the meantime , the ODataController at the RESTful WebAPI have handled the request with the ID in the ODataURI : 


The action method renders a response with code 410 "GONE" , to express that the record has been deleted , and Postman exposes it : 






That's all... 
In this tutorial we've learned how to send an HTTP DELETE Request to a RESTful  ODataController Web API Service using Postman.
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן






Sunday, March 1, 2015

How to send an HTTP PUT Request to a RESTful ODataController Web API Service using Postman

In this post we'll learn Step by step how to send an HTTP PUT Request to a RESTful ODataController Web API Service using Postman
We'll make use of the Postman tool to test a RESTful OData Web API , sending an HTTP PUT request. That  working OData Web API with ODataController , was built in a previous tutorial. After we update the  record , we check it sending an HTTP GET request :

how to send an HTTP PATCH Request to a RESTful  ODataController Web API Service using Postman



Important : An HTTP PUT request is expected to provide an object to be UPDATED , but the object MUST include all of the object's properties , not just the updated ones . If you want to send a PARTIAL object , you must use the HTTP PATCH verb instead . We explain it in this HTTP PATCH tutorial .

If you do not have Postman installed , just go to the Tools >> Extensions on the Chrome browser , make a search for "Postman" and install the App .

HTTP PUT Request to a RESTful ODataController Web API Service using Postman



Now let's check out how to call the OData Web API : in MVC , to setup  the ODataController there have to be an ODataModelBuilder at the "Register" method called from the Global.asax :  the EntitySet name MUST be called after the name of the Controller ( "Notes" in our case ) , so we'll search for a "NotesController" at the application :

Notice there is a route prefix declared ( "ODataV4" ) , which must be appended to the URI . 



Found the ODataController , now we look for the "Put" method , because we're sending an HTTP PUT request :


Why to check this ? Because we want to know which parameters is expecting the ODataController  :   it expects :
1) a "key" which is the ID of the item : must be in the URI
2) a "Note" object , which must be included in the request's body ( "[FromBody]" ) . 

How do we create a Note object ? Take a look at the declaration inside the Model :



Now we know the Note's properties and its constraints.

Alternatively , you could ask for the METADATA info , but that way you won't know the Data Annotations constraints declared at the Model :

http://localhost:21435/ODataV4/$metadata

The Port  can be obtained from the Web tab at the application properties:


Merge all of this together , and you will have built the URI for the PUT request : "http://localhost:21435/ODataV4/Notes(5)" :

Set the URI , select the PUT method , set the "Content-Type" header , and write the JSON object according to the Note's declaration:


Important : because the OData protocol is case sensitive , if you type "notes" instead of "Notes" , you will not reach the data.


Send the request : as you see , the Controller has bound the JSON object to a local Note variable :




The details of the HTTP PUT request can be seen in Postman as follows : 


 The update was made , and an HTTP GET request can verify that :



That's all... 
In this tutorial we've learned how to send an HTTP PUT Request to a RESTful  ODataController Web API Service using Postman.
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן







Monday, February 23, 2015

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman

In this article we'll learn Step by step how to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman
We'll use Postman to test a RESTful OData Web API application, sending an HTTP POST request. We'll start with a working OData Web API built on an ODataController , which we built in a previous tutorial,   and we'll create an entry using  Postman . After we create a new record , we check it sending an HTTP GET request :



HTTP POST Request to a RESTful OData Web API Service using Postman
In order to get Postman , just go to the Tools >> Extensions on Chrome , do a search for "Postman" and install the App .

HTTP POST Request to a RESTful ODataController Web API Service using Postman



Open Postman and select "POST" from the list :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman  1

Now let's learn how to call the OData Web API : in MVC , in order to setup  the ODataController , there must be an ODataModelBuilder registered at the "Register" method called from the Global.asax file : as you see in the following snapshot , the EntitySet name MUST be the name of the Controller ( "Notes" in our case ) , so we'll look for a "NotesController" ODataController at the application :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   2
Also , there is a route prefix declared ( "ODataV4" ) , so we'll append it to the URI . 


How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   3

Found the ODataController , now we check for the "Post" action method , because we're sending an HTTP POST request :


How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman     4
Why are we checking this ? Because we want to know which kind of object is the ODataController method expecting : in our case , it expects a "Note" object , which must be included in the request's body ( "[FromBody]" ) . How to create a Note object ? Look at the declaration inside the Model :


How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   5

Now we know that a Note must include all of those properties.
You could send an OData request asking for the METADATA information to construct the Note object , but that way you won't know the constraints declared with Data Annotations at the Model :

http://localhost:21435/ODataV4/$metadata

The Port of the application can be extracted from the Web tab at the application properties:

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   6

Put all of this together , and you have the URI for the POST request : "http://localhost:21435/ODataV4/Notes" :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   7

Important : OData protocol is case sensitive : therefore , if you type "notes" instead of "Notes" , you will not reach the controller.

After setting the URI and the POST method , set the "Content-Type" header :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman  8

Next , build the JSON object to be sent inside the request's body , according to the Data Annotations :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   9

Send the request :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman  10

Postman is waiting for the response :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   11

In the meantime , the ODataController at the RESTful WebAPI have gotten the JSON object and bound it to a local variable : 

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman    12
The action method renders a response with code 201 CREATED , and Postman exposes it : 


How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   13

Finally , if you want to , send a HTTP GET request to check for the new record added to the database :

How to send an HTTP POST Request to a RESTful ODataController Web API Service using Postman   14




That's all... 
In this tutorial we've learned how to send an HTTP POST Request to a RESTful  ODataController Web API Service using Postman.
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן









Thursday, September 18, 2014

How to send an HTTP PATCH Request to a RESTful ODataController Web API Service using Postman

In this post we'll learn Step by step how to send an HTTP PATCH Request to a RESTful ODataController Web API Service using Postman
We'll use Postman to test a RESTful OData Web API application, sending an HTTP PATCH request.
There is an important difference between the two methods for updating entities , HTTP PUT and HTTP PATCH.
An HTTP PUT request is supposed to provide an object to be UPDATED , such object including all of the object's properties , and not only the updated ones , as the HTTP PATCH does . If you need to send a PARTIAL object , you must use the HTTP PATCH method. Therefore , the object send is bound at the Controller to a "Delta<>" local variable , and not to an instance of the object. We'll use here  a working OData Web API  ODataController built in a previous tutorial,   and we'll update an item using  Postman   :

how to send an HTTP PATCH Request to a RESTful  ODataController Web API Service using Postman



In order to setup Postman ,  go to the Chrome Tools >> Extensions   ,   search for "Postman" and install the Application .

HTTP PATCH Request to a RESTful ODataController Web API using Postman


Now let's learn how to call the OData Web API : in MVC , in order to setup  the ODataController , there must be an ODataModelBuilder registered at the "Register" method called from the Global.asax file : as you see in the following snapshot , the EntitySet name MUST be the name of the Controller ( "Notes" in our case ) , so we'll look for a "NotesController" ODataController at the application :

Also , there is a route prefix declared ( "ODataV4" ) , so we'll write it in the URI . 



Now we check for the "Patch" action  , since we're sending an HTTP PATCH request :


Why we check this ? Because we must know which type is the ODataController expecting : in our case , it expects a "Delta<Note>" object , which is a partial generic object included in the request's body ( "[FromBody]" ) . 
Also, we must comply with the declaration inside the Model , respecting the constraints :




We could instead ask for the METADATA to construct the Note object , but that way we won't know the constraints set at the Data Annotations   :

http://localhost:21435/ODataV4/$metadata

The Port can be seen at the Web tab inside the application properties:



So set the URI , write the Content-Type at the request's Header ,  and select "PATCH" from the list  :


Then , write the JSON object , pushing the "Raw" button :



Important : since the OData protocol is case sensitive , if we type "notes" instead of "Notes" , we will not reach the controller.


Send the request : 



You can see that the Action method got the Delta<> object :


The following is an example of an incomplete partial Note object , containing ONLY 1 property to be updated :



As you see , the response is 200 OK , meaning that the item was successfully updated .

The details of the request can be seen in Postman as follows :





That's all... 
In this tutorial we've learned how to send an HTTP PATCH Request to a RESTful  ODataController Web API Service using Postman.
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן