Wednesday, February 24, 2021

Setting IB Request Content Type

In our Integration Day 2021 One-day Webinar, we showed how to send SMS from PeopleSoft through Twilio. What makes Twilio interesting is that Twilio expects URL encoded form data. As you may know from using Integration Broker, PeopleSoft is quite happy sending and receiving JSON or XML. But what about other content types, such as application/x-www-form-urlencoded? You may know that you can set most HTTP headers using something like:

&request.IBInfo.IBConnectorInfo.AddConnectorProperties("Content-Type", "application/x-www-form-urlencoded", %Header);

That's a mouth full! But the weirdest thing happens when trying that trick with URL encoded form data. PeopleSoft URL encodes the entire body! Wait, isn't that correct? No, actually. When you and I create the body, we create the proper key=value&key2=value2 pairs. Proper URL encoded form data would look something like this:

key=This+is+some+encoded+data&key2=value2

Keys, ampersands, and equals signs should not be encoded (unless part of the value). But PeopleSoft URL encodes everything as if there were no keys. Here is what that same string above looks like when PeopleSoft finishes with it:

key%3DThis%2Bis%2Bsome%2Bdata%2Bto%2Bencode%26key2%3Dvalue

It is just supposed to URL encode the values. Or better yet, just let us URL encode the values. What's the workaround?

&request.SegmentContentType = "application/x-www-form-urlencoded";

This solution is a little strange because we aren't using a multi-part, segmented message, but it works!

While performing research for this post, I came across MOS Doc ID 2187249.1, which asks for an enhancement to allow additional, hopefully even custom, Content Types, so it appears URL encoded form data isn't the only issue. But lucky for us, we have SegmentContentType as a workaround!

Are you interested in learning more about PeopleSoft Integration Broker and integration strategies? Join one of our live virtual top-rated Integration Tools Update courses!

6 comments:

Anirudh said...

I believe we can also use &MSG.IBInfo.IBConnectorInfo.AddQueryStringArg as a work around for the encoding issue. this way fields remain as separate values and not as a single string.

Sabarna said...

Using AddQueryStringArg, will work only with certain applications. not all apps accept content in query string. for e.g., Azure expects the content to be in the body.

I tried setting request content type for azure AD Oauth(client credentials grant type), it works properly until the message leaves application server, but in target connector log, the body content is fully encoded. I found this from IB msglog.

Andy said...

Hi Jim,
If I'm not mistaken, Twilio appears to use something called an Account SID as the username for basic authentication to their API. It's 34 characters.

When you put in the request headers, the Basic Authentication username field (IBEXTERNALUSERID) only allows 30 characters.
Did you happen to come across this? And wondering if you have any ideas?

Jim Marion said...

@Andy, correct. Yes, we ran into this as well. Here is a PeopleCode fragment for setting the Authorization header:

&request.IBInfo.IBConnectorInfo.AddConnectorProperties("Authorization", "Basic " | &token, %Header);

You are responsible for base64 encoding the username:password, which is &token above. We have several examples of base64 on our blog. I recommend the PET example.

Andy said...

Thanks, Jim. That helped us get past the authorization header issue. But getting stumped by something else now where the gateway logs are showing "A 'To' phone number is required..."

My REST based URL looks like this:
https://api.twilio.com/

And my URI template with Index 1 looks like this:
2010-04-01/Accounts/{acc_SID}/Messages/
(filling the acc_SID with my document template)

To pass the To phone number and other parameters in the body as x-www-form-urlencoded, I'm populating the request message:

&MSG = CreateMessage(Operation.SENDSMS_GET);
//code that populates URI document template

//populate request message
&DOC = &MSG.GetDocument();
&COM = &DOC.DocumentElement;
&COM.GetPropertyByName("To").Value = "+12222222222";
&MSG.SegmentContentType = "application/x-www-form-urlencoded";

&RESP = %IntBroker.SyncRequest(&MSG);
MessageBox(0, "", 0, 0, "%1", &RESP.genxmlstring());

But seeing this in the gateway logs "A 'To' phone number is required..."
No issues when sending it the same way in PostMan. Wondering if you have any ideas?

Jim Marion said...

Hi Andy,

The problem is that you are using a document and creating XML. The message body is supposed to be URL Encoded Form data. Documents don't support URL encoded form data. Your request message for the post should be unstructured. I like using IB_GENERIC_REST.

I recommend checking out our Integration Day 2021 replay video as it walks you step-by-step through Twilio integration and includes sample source code you can alter to satisfy your requirements. You can find the replay online at https://learn.jsmpros.com/courses/id21.