Consuming Restful webservice using Http Request endpoint

Mule 4 Indepth Consuming Rest and Soap Webservices
23 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

Welcome back. In this video, we will understand how to consume a REST API and how to pass some arguments, REST API s, etc. Remember, in this video, I'm going to show you only how to consume we are not going to expose the REST API right now. Later, we'll do it okay. So, how to consume to show you how to consume let me show you an existing REST API which I have. So, in this lab documents which I have provided to you in this lab documents section download, I have given a jar file called as product service rest.

So this is a jar file, which I have created using spring framework. Don't worry how I have created the REST API. We will try to understand how to actually consume it. So I have written this jar file is in Spring Boot. And I have written one more bat file called as rescue dot bat. What is this having Let me open.

Here it is just executing the Java hyphen jar command. That means executing the jar and then configuring a server port as 7070 and context Potter's slash rest. So once I execute this, my Java program will start an embedded Tomcat running at 7070 port, and the root URL format for accessing my application is slash rest. So let me start, I'll click on this red dot bat. It's executing Java in Java, which is nothing but my spring application which I have written. Now let me show you how to consume it.

So I'll go to postman just do here localhost. 81 slash sonority 81. What is it the Port 7070 slash rest, slash? Actually, my REST API is for exposing some product details. So in my REST API, there is a, there is an endpoint mapper to slash products. Slash products.

Slash rest is a base URL. If we give a get request to this one, let us see what my REST API is returning. Yep, it returned me a list of products in JSON format. If you see this isn't a straight list or an array, this is one product. So whenever I give a get request to slash products, it's listing all products. Actually.

It is displaying products up to ID six. Suppose if I want a product by name I want let's say HP products. My there's one REST API endpoint called slash product slash the product name HP if I say it gave me only one HP product and suppose if I say slash Mac it should show me all the Mac products. So there are I think two products. Let us see. Yes, and a list of phase two, which is showing me only Mac products.

So slash products GET request is giving me all products slash product slash product name, in the section product by name. Now, I want to add a new product. See, this is the Jason of a product how it looks like if I want to add a new product. My API, I have written the REST endpoint to take post request to slash products. And since it is public Request surpass body, raw body. Fish this whenever I'm posting a product, I don't have to give the product ID and say your name just to distinguish see web book laptop and then a MIME type of the body is what?

Jason so select Jason sent. So this should add new product to the existing products. So I got a response saying the product IDs server. Now, let me give a get request to slash products. GET request I give. See, I got a list of products will there be product 237 server book laptop?

Yes. So there are three operations in my REST API. Slash products. Get requests will get all the products, slash products. slash product name will you by name and post to slash products, we'll add a product. Now I want to consume this using mule till now we have consumed using postman.

So let us see how to do it. So I'll be working on a project called as consuming rest start project. Here I have an XML file open this. This is an empty XML, right. Now, I want to drag a HTTP request HTTP listener is for listening for HTTP requests. So if you want to make HTTP requests, your drag and drop request and drag and drop.

Yes, so a flow has been created with a request in the process, but see the request is not in the source source part will contain only the inbound endpoints, so I'll just double click on the flow I'll get a flow name as get all products. So this is how we can rename the flow also. See the flow name is renamed as get all products. And this is a HTTP request or operation. This requires configuration. I'll click on plus.

Here, we need to tell what is the protocol, HTTP, or the host, local host at what port is my REST API listening 7070 and the best part is rest. So 7070 and then best path here is asking slash rest. So I can't figure all this. I'll take all the other says defaults and say okay, So now for this HTTP request connector configuration is configured. I told what is the base URL localhost 7070 slash rest. Now, I want this HTTP requester to make a get request, so I here select Get AND GET request to what slash products that's up.

If I want to pass anything as part of body, I just maximize it. Here I can pass or expression. The output of this expression will be sent as body. If I want to add any headers to the outgoing request, I can add headers, query parameters, you are a parameters, etc. In this case, I don't want to pass anything, I just want to make a get request to slash products. Let's see.

To test this. I'll drag and drop one more flow with HTTP listener. I'll configure the connector configuration by clicking on plus. So here I want the listener to listen on 8081 Okay. Then here path us slash products. So, if I give a get request to localhost 8081 slash products this flow gets kicked off how to invoke one flow from another flow very simple there is something called as flow reference.

I will go to core and here is flow reference. This is core module. Here I have for reference I'll drag and drop it here. Then here at tell the name which I have to refer to get all works. Okay. And I want to rename this first floor also as main claw.

I selected the floor And then I'll give main flow Okay, so from the main flow I'm actually invoking get all products flow, get out products flow is making HTTP requests to slash record slash products. So let me just try to run it and check and run it again. Okay, the application looks like deployed now I'll give a request to localhost 8081 slash products, check. Yep. So I got the same response. We are there are seven products.

I got same response just like how I got by hitting the original Stick. In a normal scenario once we get response from REST API, we might want to apply some transformations and do further pricing. We'll see what detailed trans transformations and all later. But just here in this case, I showed you how to consume a basic REST API. But here, I consume the REST API where I just configured it to make a get request slash products. I want products by name.

So I'll configure one more flow, sold drag and drop and more HTTP request. I will rename this flow using get by name. Okay, then, here the request. I don't know how to configure a new request configuration. I want to reuse the existing one Because anyway the base party same localhost 7070, slash rest. And I want to give a get request toward slash products slash product name is it slash products?

Slash I don't want to hard code, the product name. I'll give a parameter in the UI. This is called as you are I parameter in curly braces. I'll say p name. Okay, this is just a placeholder p name. Either you are I parameter a parameter coming into you or I.

Okay, now how to use a value for you or a parameter. So actually, what I'm assuming is whenever somebody is making a request to 8081 slash products, if the pass questionmark query parameter product name is equals to HP I want to display only HP products. So I'm expecting a query for metric collars product name, I want to pass it to the REST API. So here what I will do is I want to get the value for p name. So I selected URL parameters. I'll add plus key is p name, whatever I gave as a name for a parameter and the value is how to retrieve the query parameter.

You already know the expression. Attributes dot. I will write an expression like this hash, three fields dot query pair Rams dot product name. I'm extracting this value from the mule message, setting it as a value for P. Let us check if that works. Now to test this, what I will do is in this flow reference right now is affecting get all products as select Get by name, display name. To be very clear, I'll be the same get by name.

So once I hit my main floor right now, it will hit get by name, which is referencing to get by name. I can see the console. Once I saved my XML automatically, it should have got redeployed. Now I'll go to postman and we'll do a request send. I should get only the products. Yeah, what it's saying cannot use a parse expression.

So let us see the error on the server side and supported operation here. It was an evil two evaluate this right? Very nice. So actually, what I'll do is here when I was giving the value when I was giving the value for you or a parameter, I have intentionally kept here. Oh, it was not saved. Very good.

I will login configure pin in another value is right as at tributes dot query parents dot Name Is it the same what I was passing? Okay there's a way to extract the query parameter by writing a simple expression right. So I wrote without hash right now. Remember, I had to save it here, apply changes a lot. I didn't click on this. So I saw an error.

Now let me check in XML. Here p name. The value is attributes dot query params dot product name earlier, aka hash. Now I gave without hash. So let me check in a console. You should have got redeployed right now.

Okay, I'll clear the console for 20. Oh, let me give a request. Yep, I got the response. There is only one product for HP, LG product name is equals to Mac. Then say I got the response for Mac products, right. So We understood how to consume a REST API which expects you are a parameters.

So there are two flows right now, one flow is actually configured to make a get request to slash products. In under flow, it is configured to make a get request to slash products slash p name p name is a URL parameter. and the value for URL Param. Here I gave p name. It is it is accurately from attributes. So whenever you're writing the URL params remember that they will become key value base in this expression.

You don't need to write hash. Remember that is a common mistake which many people will do okay. So I have two flows, but here I was able to invoke any of the flow at once how to invoke the flows conditional Yes, if you want to conditionally invoke the flows, what you can do is I will stop this application right now, by clicking on this red button. What you can do is you can delete this flow reference, there is one router called as choice and drag and drop choice router after HTTP listener. And here what I will do is I'll drag and drop to floor references, one floor of here and another floor reference again, after choice. And in default, I will drag and drop set payload.

Here I will set the payload as simple text invalid Request Okay. Now, the first floor reference, I wanted to point to a flaw with name get all products algorithm display names accordingly Okay, second floor reference, I want to point to get by name. I will rename this also using get by name. Now, the floor reference sorry the choice outer is having three routes, actual choice routers like switch case default, these two ones are nothing but two cases and this is default. Now I have a choice router when to route to each route. I will double click on the choice maximize it.

Yeah, why not here? I will double click on the ven cure here when I die conda when out write an expression when it should be executed. So, what I will do is here I will write a simple expression attributes dot query params dot product name is equals to null. That means, if there is no product name as query parameter then I won't get all products to be executed. Here I will write the reverse get by name I will write the reverse if there is if the product name is not null then I want the second floor reference. So, I conditionally configured which one should be which floor should be executed by using choice route.

Let me run it oh there are some errors it is saying what is the rough We'll go to the XML and see if it will be easy to see an error. Let me give him space Yep. After I gave the space it was okay. Now let me deploy this okay it got deployed. Now let me give a request by passing a query parameter product name. I should get the Mac products only right now.

Yes. And if I don't pass product name is query parameter. I should get all the products. Let's see. Yep, I got all the products, seven products right. So I was able to conditionally route by using Try sock.

Nice. So in this video is how to consume a REST API, which simply requires a URL parameter. Right? One more thing I will tell you before we stop. What I want is in this get by name. Here, I was passing p name right as a URL parameter.

I was extracting URL parameter value from attributes or query parameters or production a small modification I do just for understanding. I'll set a variable. In this get by name, I'll drag and drop set variable. Here I want to set a variable with the name, product name, product name. and the value is this expression. attributes are query params dot fragment.

So it means I'm getting the value of the query parameter product name and then putting it as problem. Now, can you check what I have to do here? Can you guess? here for the value of p name? What I can do is I can say Where's Courtney? That's can be used to extract a variable, right?

That's it. So we're just giving them error whatever. Actually, there is nothing here. Sometimes even though there is no error to show like that, but let me check in XML. Is it really an error? No, an XML is not showing.

So once I save this, there should go. Yep. Sometimes Don't worry in any point story or even if there is no error rate, So as an error, you just had to save the XML file by just typing some tab or space anywhere. There's a problem with any point to do sometimes. Now I think the application is already running. Right deployed and I should get same response if I give products by name, question mark.

Product Name is HP I got alright. So, I showed you practically how to set a variable. See you next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.