cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CPI: use Query parameters in external API call

anhuhn
Discoverer
17,182

Hi

I'm still a little new to CPI so I hope my question is not to trivial. Anyways, I have built a little IFlow that is supposed to be triggered via postman with some query parameters, for instance along the lines of:

https://p123456-iflmap.hcisbp.eu2.hana.ondemand.com/my/address?name1=1234&name2=test

I want my IFlow to take these parameters (name1=1234 and name2=test) and rename them and add them to a header I'm sending to an API with a RequestReply adapter, so the IFlow's request to the API looks something like:

https://my.api/adress?name3=1234&name4=test

I don't want to use the same parameter names in the postman request as in the API call, since some of the API parameter names are unintuitive, hence the renaming.

While I'm at it, I think I should also remove all other query parameters from the message header, so no unexpected behaviour happens, if the postman user "guesses" the name of one of the parameters of the API and makes a request like:

https://p123456-iflmap.hcisbp.eu2.hana.ondemand.com/my/address?name1=1234&name2=test&name3=4567

anyways, my assumption is that this is quite easy to solve with a groovy script or maybe even a Content modifier, but unfortunately I'm not sure how and don't seem to be able to find any solutions online.

Thanks in advance for your help

Accepted Solutions (1)

Accepted Solutions (1)

anhuhn
Discoverer

Alright, I think I figured it out.

Inspired by Handle Multiple input request parameter in CPI I wrote the following groovy script:

/* Refer the link below to learn more about the use cases of script.
https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/148851bf8192412cba1f9d2c17f...

If you want to know more about the SCRIPT APIs, refer the link below
https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/index.html */
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def params = [:]
(message.getHeaders().CamelHttpQuery =~ /(\w+)=?([^&]+)?/)[0..-1].each{
params[it[1]] = it[2]
}

message.setProperty("name1", params.name2)
message.setProperty("name2", params.name2)
message.setHeader("CamelHttpQuery", "")

return message
}

I empty the CamelHttpQuery at the end, to make sure I don't get any unexpected behavior when I call the API down the line. Though I suspect there might be a prettier solution for this.


Then it's it's just adding name3=${property.name1}&name4=${property.name2} to the query section of my external HTTP call.

Hope this might help someone else.

Answers (3)

Answers (3)

martinfriebe
Product and Topic Expert
Product and Topic Expert

Hello Andre,

in short all your querys are added as header parameter by default but only if you allow them in runtime configuration.

1. please see runtime configuration to allow headers (accept all incoming headers with *)

2395225 - Incoming HTTP Header values are not reflected in the iFlow - SAP for Me

2. please see sap help for http sender adapter to see how to send querys in url

HTTPS Sender Adapter | SAP Help Portal

in the end it could look like this:

while using request replay you can use following adress:

https://my.api/adress?${property.name3}&${property.name3}
anhuhn
Discoverer
0 Likes

EDIT: Nevermind, I think I have figured it out (see above)


Hello Martin, thanks for the quick reply.

Unfortunately, unless I've made a mistake, this doesn't quite fix my problem. From what I understand AllowedHeader(s) only controls which Headers get through to my iFlow. However I want to pass name1 and name2 as query parameters (in the Params tab in Postman)

So in the Header of my iFlow they show up in the CamelHttpQuery line as, for example, "name1=1234name2=test". So I suppose I would like to have some way of extacting them into Exchange Properties from there.

Thanks in advance

jbans
Discoverer
0 Likes

1. Allow the headers in the runtime configuration. Using a asterick to permit for all sometimes causes issues.
So maintain the name as it is like in the screenshot. The "|" comes in between.

Screenshot 2024-07-28 at 12.19.57 AM.png

 

 2. Now go to the content modifier>Message Header tab and create the names and values for both params.

Screenshot 2024-07-28 at 12.23.02 AM.png

3. Now go to the content modifier>Exchange Property tab and create the names, choose header for source type and for source value pick the corresponding value in the dropdown.

Screenshot 2024-07-28 at 12.25.23 AM.png

 4. Go to the target system tab and in both query params pass ${property.params} as values
and for the request headers pass the same names you allowed in the headers in the runtime configuration.
Save and deploy service @anhuhn 

Screenshot 2024-07-28 at 12.29.46 AM.png

AlanWaters
Participant
0 Likes

Hi Andre,

If I understand you correctly, you want to take the Header values that were passed in the query and assign them to Exchange Properties? We do this all the time. Simply use a Content Modifier to map from Source Type = Header with the Source Value = header name to a property:

The values from the headers will now be available as Exchange Properties.

Hope this is what you're after.

Thanks,
Alan