API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  How to invoke target-url / api with multipart form data parameters

    Posted Thu October 13, 2022 09:35 AM
    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
    ------------------------------


  • 2.  RE: How to invoke target-url / api with multipart form data parameters

    Posted Fri October 14, 2022 09:54 AM

    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
    ------------------------------



  • 3.  RE: How to invoke target-url / api with multipart form data parameters

    Posted Fri October 14, 2022 09:58 AM
    Hi Steve:

    Yeah, I'm aware it went out of service, we are in the process of upgrading it.

    Thank you for your help, I'll try your solution and let you know.

    Regards.

    ------------------------------
    Joan Hache
    ------------------------------



  • 4.  RE: How to invoke target-url / api with multipart form data parameters

    Posted Fri October 14, 2022 10:07 AM

    Hi Joan,

    You're welcome, let me add that the latest version of the AMU will run this same "normalization" when migrating GatewayScript / xslt source from the v5 to the v10 API Gateway service, so this "workaround" will be automatically removed.  The v10 native gateway doesn't do this "normalization" so it would have caused a compile error otherwise.
    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 5.  RE: How to invoke target-url / api with multipart form data parameters

    Posted Fri October 14, 2022 04:25 PM
    Edited by Joan Hache Fri October 14, 2022 04:29 PM
    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
    ------------------------------



  • 6.  RE: How to invoke target-url / api with multipart form data parameters

    Posted Wed October 19, 2022 01:50 PM

    Hi Joan,

    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?

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------