As for the difference in length between what is sent from your generated multipart form above via DataPower and postman, is the boundary used by postman the same length and the one you built? I do see that your Content-Type headers have a space before the name, ie \\n<space>Content-Type:
for the client_secret and resource, so that could be a difference in your postman request. Perhaps this makes the client_secret and resource to be not found by the backend resulting in the authorization failure?
Original Message:
Sent: Fri October 14, 2022 04:24 PM
From: Joan Hache
Subject: How to invoke target-url / api with multipart form data parameters
Hi there Steve:
Your suggestions did solve the compile problem, but now it looks like the backend service is not receiving the parameters because I'm receiving as response the sign in page, which means that the service didn't received the credentials.
I dont know if the paramAsBlob sintax that I posted beofre is correct, I made some changes to it and now looks like this:
var paramAsBlob = '--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="grant_type"\\nContent-Type: text/plain\\n\\n'+grant_type+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="client_id"\\nContent-Type: text/plain\\n\\n'+client_id+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="client_secret"\\n Content-Type: text/plain\\n\\n'+client_secret+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="resource"\\n Content-Type: text/plain\\n\\n'+resource+'\\n--------------------------948993005467454460911999--' ;
Oh, one more thing, when I run the backend service in postman it responds ok, and the content length size is 720 somehow. When the program do the paramAsString.length it only gets 692
------------------------------
Joan Hache
Original Message:
Sent: Fri October 14, 2022 09:54 AM
From: Steve Linn
Subject: How to invoke target-url / api with multipart form data parameters
Hi Joan,
The API Connect v5 (and v5 compatible for v10) gateways have an anomaly of changing escaped characters such as \n into an actual character, ie a newline x0A character. Depending upon the location of this character that can be a good thing, but if within a JavaScript string, it splits the string across two lines which causes the compile error.
For example
var test1 = 'hello ' + \n 'world';
will normalize to
var test1 = 'hello ' +
'world';
which will compile, but
var test1 = 'hello \n world';
will normalize to
var test1 = 'hello
world';
which will not compile. This is your use case.
Given the potential for breaking existing customers that take advantage of this "feature", the advice provided to customers is to change your \n
to a \\n
in your GatewayScript source. The gateway will normalize that back to the desired \n
and the code will compile and execute properly.
var paramAsBlob = '--------------------------948993005467454460911999\\n'+'Content-Disposition: form-data; name="grant_type"\\n\\n'+grant_type+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="client_id"\\n\\n'+client_id+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="client_secret"\\n\\n'+client_secret+'\\n--------------------------948993005467454460911999\\nContent-Disposition: form-data; name="resource"\\n\\n'+resource+'\\n--------------------------948993005467454460911999--' ;
Having said that, you do realize that API Connect version 5 went out of service on April 30 of this year, with extended service only to those with contracts to the end of this year. There is a migration tool, the API Connect Migration Utility (AMU) that will migrate your v5 configuration into either a native gateway implementation or a v5 compatible implementation in v10. The key difference from v5 will be the infrastructure for the API Manager and DataPower may be kubernetes based, although you can also have virtual and physical DataPower appliances as well. There's plenty of documentation on this in the v10 knowledge center.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Thu October 13, 2022 09:04 AM
From: Joan Hache
Subject: How to invoke target-url / api with multipart form data parameters
Hi there:
I want to invoke target url with some multipart-form data parameters, I tried passing the parameters using gateway script, to then call the url with a invoke policy, all Im getting is Invalid or unexpected token error. The Api Connect version I'm using is 5.86.
I'll apreciate your time and help in advance.
Here is the code I used:
var apim = require('./apim.custom.js'); apim.setvariable('message.headers.Content-Type','multipart/form-data; boundary=--------------------------948993005467454460911999');apim.setvariable('message.headers.accept','application/json;odata=verbose');var grant_type = apim.getvariable('api.properties.property1');var client_id = apim.getvariable('api.properties.property2');var client_secret = apim.getvariable('api.properties.property3');var resource = apim.getvariable('api.properties.property4');var paramAsBlob = '--------------------------948993005467454460911999\n'+'Content-Disposition: form-data; name="grant_type"\n\n'+grant_type+'\n--------------------------948993005467454460911999\nContent-Disposition: form-data; name="client_id"\n\n'+client_id+'\n--------------------------948993005467454460911999\nContent-Disposition: form-data; name="client_secret"\n\n'+client_secret+'\n--------------------------948993005467454460911999\nContent-Disposition: form-data; name="resource"\n\n'+resource+'\n--------------------------948993005467454460911999--' ;var paramAsString = paramAsBlob.toString();var longitud = paramAsString.length;apim.setvariable('message.headers.Content-Length',longitud);apim.setvariable('message.body', paramAsString,'set');
------------------------------
Joan Hache
------------------------------