Passing Parameters to RESTRequest

Employee

Passing Parameters to RESTRequest

Hello

 

I am trying to develop a Job that exposes a rest end point and this end point tales in two parameters:

  1. areaCode
  2. groupNumber

I want to use these parameters later on in the flow to restrict a SQL Statement.

 

I have defined a 'tRESTrequest' and have added a API Mapping with a URI Pattern of "/{areaCode}/{groupNumber}/". In the Output Flow I have defined the two parameters.

 

 

tRESTRequest1.PNG

tRESTRequest2.PNG

When I execute this, I pass the two parameters

 

executing_rest.PNG

 

However the two parameters are evaluated to null. I have put a Java component to debug the the Request Object and it confirms that they are null.

 

The tJavaRow Component code is:

 

System.out.println("Area Code   : "+globalMap.get("groupCode.areaCode"));
System.out.println("Group Number: "+globalMap.get("groupCode.groupNumber"));

//The object with the key "restRequest" is a HashMap
java.util.Map<String, Object> myMap = ((java.util.Map<String, Object>)globalMap.get("restRequest"));

//Print out of what you are seeing already
System.out.println("MyMap = " +myMap.toString());

//Create an iterator over the keyset
java.util.Iterator<String> it = myMap.keySet().iterator();

//Iterate over the keyset and print out the keys
while(it.hasNext()){
	System.out.println(it.next());
}

//Retrieve the PARAMS HashMap
java.util.Map<String, Object> myParams = ((java.util.Map<String, Object>)myMap.get("PARAMS"));

//Create an Iterator for the PARAMS HashMap
java.util.Iterator<String> it2 = myParams.keySet().iterator();

//Iterate over the PARAMS HasMap
while(it2.hasNext()){
	System.out.println(it2.next());
}

The output is:

executing_rest2.PNG

 

Am I missing some config ?

 

Any Ideas

 

Thanks

 

Patrice


Accepted Solutions
Employee

Re: Passing Parameters to RESTRequest

Okay I think I resolved it, when referencing REST Inbound parameters, you do NOT need to use the 'globalMap.get'.

 

You just refer directly...

 

FROM WalkDetails "+
"WHERE WalkDetails.Group_Code = '"+groupCode.areaCode+groupCode.groupNumber+"'"

 

This worked...

 

Anyway...I have another question.

 

I want to add a check after the SQL Query and count number of rows returned, if 0 then I want the REST Response to return a custom message saying "Now rows found etc"

 

Is this possible...

Sixteen Stars

Re: Passing Parameters to RESTRequest

That is pretty much what I was trying to say with......"The AreaCode and GroupNumber are not stored in the globalMap. They are columns in the row from your tRESTRequest component."


All Replies
Sixteen Stars

Re: Passing Parameters to RESTRequest

The AreaCode and GroupNumber are not stored in the globalMap. They are columns in the row from your tRESTRequest component.

 

I assume you are using globalMap.get("groupCode.areaCode")) and globalMap.get("groupCode.groupNumber")) in your query which is returning nothing. You can fix this by adding the following code to the very top of your tJavaRow....

 

globalMap.put("groupCode.areaCode",groupCode.areaCode);
globalMap.put("groupCode.groupNumber",groupCode.groupNumber);

Of course, a better way to resolve this is to remove the tJavaRow and use the globalMap variables automatically created by the tFlowToIterate. They will have the keys.....

 

"row3.areaCode"
"row3.groupNumber"

Employee

Re: Passing Parameters to RESTRequest

Thanks Richard.

 

That is exactly how I had it originally. I only added the tJavaRow in order to debug the inbound parameters.

 

So I now deactivate the Java Component and rewire the flow...

 

tRESTRequest3.PNG

 

So when I run the flow and pass : http://localhost:8088/walkDetails/ES/50/

 

As you can see the SQLInput component returns 0 rows..

 

The SQL Statement in the 'GetWalkDetails_GroupCode' component looks like this:

 

 

"SELECT WalkDetails.WalkID,
		WalkDetails.D_grade,
		WalkDetails.D_strand,
		WalkDetails.D_event,
		WalkDetails.D_festival,
		WalkDetails.D_restriction,
		WalkDetails.Title,
		WalkDetails.Summary,
		WalkDetails.Group_Code,
		WalkDetails.WalkDescription,
		WalkDetails.Date,
		WalkDetails.Distance_KM,
		WalkDetails.Distance_Miles,
		WalkDetails.Grade_Local,
		WalkDetails.Linear_Ind,
		WalkDetails.Time,
		WalkDetails.GridRef,
		WalkDetails.Easting,
		WalkDetails.Northing,
		WalkDetails.Latitude,
		WalkDetails.Longitude,
		WalkDetails.Postcode,
		WalkDetails.StartingPointDescription,
		WalkDetails.Display_Ind,
		WalkDetails.Postcode_Latitude,
		WalkDetails.Postcode_Logitude,
		WalkDetails.CreationSource,
		WalkDetails.CreationDate,
		WalkDetails.MaintenanceSource,
		WalkDetails.MaintenanceDate
FROM	WalkDetails "+
"WHERE WalkDetails.Group_Code = '"+globalMap.get("groupCode.areaCode")+globalMap.get("groupCode.groupNumber")+"'"

This should return some rows, because if I were to change the SQL and hard code the values that I pass via the URL, the SQL returns rows

 

 

		WalkDetails.MaintenanceSource,
		WalkDetails.MaintenanceDate
FROM	WalkDetails "+
"WHERE WalkDetails.Group_Code = 'ES50'"

and run the flow...

executing_rest3.PNG

 

 

 

Let me know

 

Thanks for your help so far...

Employee

Re: Passing Parameters to RESTRequest

Also I have tried, casting the globalMap return object to String before concatenating it... 

 

"WHERE WalkDetails.Group_Code = '"+((String)globalMap.get("groupCode.areaCode"))+((String)globalMap.get("groupCode.groupNumber"))+"'"

 

this also did not work....

Sixteen Stars

Re: Passing Parameters to RESTRequest

Have you tried printing....

 

((String)globalMap.get("groupCode.areaCode"))+((String)globalMap.get("groupCode.groupNumber"))

...to the System.out? Are you SURE it will produce a value that will return records? 

Employee

Re: Passing Parameters to RESTRequest

Hi, There was an option to provide query params in the component API mapping.
look in component documentation “query“
And add the the 2 fields you are looking for in the schema as comment col (query)
Employee

Re: Passing Parameters to RESTRequest

Okay so I have simplified it and just add one parameter  and just for a minute lets forget the SQL...All I want is to pass a parameter and have the flow acknowledge the receipt of that parameter and print the value that I passed....

 

one_param.PNG

one_param2.PNG

 

I have added a Java Component to print the value of this parameter..

 

java.PNG

 

when I run this...

 

executing_rest4.PNG

 

the output shows that the values in the parameters are null.

 

executing_rest5.PNG

Employee

Re: Passing Parameters to RESTRequest

I have added the 'query' in the comment field in the schema...the screen shot shows it.

Employee

Re: Passing Parameters to RESTRequest

Append to your url in browser ?param=value
Employee

Re: Passing Parameters to RESTRequest

I tried these ...

 

http://localhost:8088/walkDetails?areaCode=ES50

http://localhost:8088/walkDetails?param=ES50

http://localhost:8088/walkDetails/areaCode=ES50

 

none of them picks up the value of 'ES50' and passes it to the REST...

Employee

Re: Passing Parameters to RESTRequest

Okay I think I resolved it, when referencing REST Inbound parameters, you do NOT need to use the 'globalMap.get'.

 

You just refer directly...

 

FROM WalkDetails "+
"WHERE WalkDetails.Group_Code = '"+groupCode.areaCode+groupCode.groupNumber+"'"

 

This worked...

 

Anyway...I have another question.

 

I want to add a check after the SQL Query and count number of rows returned, if 0 then I want the REST Response to return a custom message saying "Now rows found etc"

 

Is this possible...

Sixteen Stars

Re: Passing Parameters to RESTRequest

That is pretty much what I was trying to say with......"The AreaCode and GroupNumber are not stored in the globalMap. They are columns in the row from your tRESTRequest component."

Employee

Re: Passing Parameters to RESTRequest

Ah I see. Thanks Richard.