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.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
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.