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
€65.39
List Price:  €93.41
You save:  €28.02
£55.92
List Price:  £79.90
You save:  £23.97
CA$96.01
List Price:  CA$137.16
You save:  CA$41.15
A$107.15
List Price:  A$153.08
You save:  A$45.93
S$95.13
List Price:  S$135.90
You save:  S$40.77
HK$547.14
List Price:  HK$781.66
You save:  HK$234.52
CHF 63.86
List Price:  CHF 91.23
You save:  CHF 27.37
NOK kr775.40
List Price:  NOK kr1,107.76
You save:  NOK kr332.36
DKK kr487.78
List Price:  DKK kr696.86
You save:  DKK kr209.07
NZ$118.01
List Price:  NZ$168.60
You save:  NZ$50.58
د.إ257.06
List Price:  د.إ367.24
You save:  د.إ110.18
৳7,680.49
List Price:  ৳10,972.60
You save:  ৳3,292.11
₹5,842.03
List Price:  ₹8,346.11
You save:  ₹2,504.08
RM332.86
List Price:  RM475.54
You save:  RM142.67
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,491.96
List Price:  ₨27,846.85
You save:  ₨8,354.89
฿2,586.09
List Price:  ฿3,694.58
You save:  ฿1,108.48
₺2,265.39
List Price:  ₺3,236.41
You save:  ₺971.02
B$363.53
List Price:  B$519.35
You save:  B$155.82
R1,302.64
List Price:  R1,861
You save:  R558.35
Лв127.90
List Price:  Лв182.73
You save:  Лв54.82
₩96,270.48
List Price:  ₩137,535.16
You save:  ₩41,264.67
₪262.29
List Price:  ₪374.71
You save:  ₪112.42
₱4,033.94
List Price:  ₱5,763.02
You save:  ₱1,729.07
¥10,867.12
List Price:  ¥15,525.12
You save:  ¥4,658
MX$1,187.12
List Price:  MX$1,695.96
You save:  MX$508.84
QR254.93
List Price:  QR364.20
You save:  QR109.27
P994.08
List Price:  P1,420.18
You save:  P426.09
KSh9,360.69
List Price:  KSh13,372.99
You save:  KSh4,012.30
E£3,358.63
List Price:  E£4,798.26
You save:  E£1,439.62
ብር4,003.77
List Price:  ብር5,719.92
You save:  ብር1,716.15
Kz58,546.63
List Price:  Kz83,641.63
You save:  Kz25,095
CLP$67,216.99
List Price:  CLP$96,028.39
You save:  CLP$28,811.40
CN¥506.70
List Price:  CN¥723.89
You save:  CN¥217.19
RD$4,073.53
List Price:  RD$5,819.58
You save:  RD$1,746.04
DA9,418.34
List Price:  DA13,455.35
You save:  DA4,037.01
FJ$158.31
List Price:  FJ$226.17
You save:  FJ$67.86
Q543.96
List Price:  Q777.12
You save:  Q233.16
GY$14,650.29
List Price:  GY$20,929.88
You save:  GY$6,279.59
ISK kr9,815.39
List Price:  ISK kr14,022.59
You save:  ISK kr4,207.20
DH707.71
List Price:  DH1,011.06
You save:  DH303.35
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден4,025.24
List Price:  ден5,750.59
You save:  ден1,725.35
MOP$563.96
List Price:  MOP$805.69
You save:  MOP$241.73
N$1,304.33
List Price:  N$1,863.42
You save:  N$559.08
C$2,570.38
List Price:  C$3,672.13
You save:  C$1,101.75
रु9,397.27
List Price:  रु13,425.24
You save:  रु4,027.97
S/263.43
List Price:  S/376.35
You save:  S/112.91
K270.11
List Price:  K385.89
You save:  K115.77
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,873.89
List Price:  ZK2,677.10
You save:  ZK803.21
L325.37
List Price:  L464.84
You save:  L139.46
Kč1,643.47
List Price:  Kč2,347.91
You save:  Kč704.44
Ft25,458.03
List Price:  Ft36,370.18
You save:  Ft10,912.14
SEK kr764.90
List Price:  SEK kr1,092.76
You save:  SEK kr327.86
ARS$61,327.27
List Price:  ARS$87,614.14
You save:  ARS$26,286.87
Bs483.57
List Price:  Bs690.85
You save:  Bs207.27
COP$273,218.78
List Price:  COP$390,329.27
You save:  COP$117,110.49
₡35,710.66
List Price:  ₡51,017.42
You save:  ₡15,306.75
L1,733.65
List Price:  L2,476.75
You save:  L743.09
₲524,442.73
List Price:  ₲749,236.02
You save:  ₲224,793.28
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł283.24
List Price:  zł404.64
You save:  zł121.40
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.