DataPower

DataPower

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
Expand all | Collapse all

How can I post parameters and add parameters in gatewayscript urlopen?

  • 1.  How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Sat September 12, 2020 11:09 AM

    I need to call following backend service APIs from DataPower gatewayscript urlopen

    Call the service login API with POST parameters (userid and password as request parameters) to get login cookie, same as when we call a service using POST parameters using SoapUI.

    1. Call the service upload file API with http post multipart form method.
    2. Call the service download file API with http get method
    3. Can you tell me or better give me working example how to prepare options (example how to add parameters like userid, password, and how to add http headers like content-type and etc) for each of above call in gatewayscript urlopen call options?

    Thank you very much

    Tang



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Mon September 14, 2020 06:55 AM

    Hi,

    there are pretty good examples in knowledge center:

    https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.5.0/com.ibm.dp.doc/urlopen_js.html

    Just scroll down to the examples part and see if you can make them work for your use case.

    --Hermanni



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Mon September 14, 2020 07:18 PM

    Hi Hermanni,

    Thanks a lot for your information. I didn't find how to prepare parameters for my userid=blah, password=blah in options objects for me to post them as parameters to function as I use SoapUI to post my userid and password parameters for Media-Type as application/x-www-form-urlencoded.

    If you can find how to do this in options object for url-open, that will be helpful for me.

    Thanks,

    Tang



    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Tue September 15, 2020 07:06 AM

    Is it possible for you to just put the parameters into the URL? Like this:


    var urlopen = require('urlopen');


    var options = {

          target: 'http://api.example.com/myserviceuri?userid=blah&password=blah',

          method: 'post',

          headers: { 'X-My-Header1' : 'value1' , 'X-My-Header2' : 'value2' },

        contentType: 'text/plain',

          timeout: 60,

     sslClientProfile: 'aliceProxyForwardTrusted',

           data: "Hello DataPower GatewayScript"};


    urlopen.open(options, function(error, response) {

     if (error) {

      // an error occurred during the request sending 



    #DataPower
    #Support
    #SupportMigration


  • 5.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Tue September 15, 2020 12:06 PM

    Hi Hpernaa,


    I tried by adding userid and password in url like

    http://host:80/core/loginguest?userid=blah&password=blahpwd

    and it didn't work.

    I even tried to add

    options={

    userid: 'blah',

    password: 'blaspwd'

    …..

    }

    but it still didn't work.


    Any ideas?


    Thanks,


    Tang



    #DataPower
    #Support
    #SupportMigration


  • 6.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Tue September 15, 2020 12:48 PM

    Can you explain what do you mean by POST parameters? When you use SOAPUI where do you place your userid and password? In payload or do you use the parameter section? If you use the parameter section what is the type of you parameter? Query or header or something else?

    --Hermanni



    #DataPower
    #Support
    #SupportMigration


  • 7.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Tue September 15, 2020 01:59 PM

    Hi Hermanni,


    In SoapUI test, we use parameters to specify userid and password, not in body. and Media Type is application/x-www-form-urlencoded by selecting checkbox for post querying string.


    Thanks,


    Tang



    #DataPower
    #Support
    #SupportMigration


  • 8.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Wed September 16, 2020 08:44 AM

    Ok. I tested this with SOAPUI and it puts the parameters into POST payload when you select "Post QueryString" checkbox. Try to put your creds into data attribute and not into the URL. And then set Content-Type header to "application/x-www-form-urlencoded":


    var urlopen = require('urlopen');


    var options = {

          target: 'http://api.example.com/testservice',

          method: 'post',

          headers: { 'X-My-Header1' : 'value1' , 'X-My-Header2' : 'value2' },

        contentType: 'application/x-www-form-urlencoded',

          timeout: 60,

     sslClientProfile: 'aliceProxyForwardTrusted',

           data: "userid=blah&password=blah"};


    urlopen.open(options, function(error, response) {

     if (error) {

      // an error occurred during the request s



    --Hermanni



    #DataPower
    #Support
    #SupportMigration


  • 9.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Wed September 16, 2020 08:20 PM

    Hi Hermanni,


    After I use the way you give above, I got following errors


    3:16:02 PM: URL open: A URL open error occurred when sending the request to the target 'https://doisftp.filecloudonline.com:443/core/loginguest', status code: 8.

    3:16:02 PM: Error occurred when connecting to URL 'https://doisftp.filecloudonline.com:443/core/loginguest'

    3:16:02 PM: The header is empty when connecting to URL 'https://doisftp.filecloudonline.com:443/core/loginguest'.


    I searched google with "The header is empty when connecting to URL". I didn't find any useful information. Do you have any ideas how to fix this issue?


    Thanks,


    Tang



    #DataPower
    #Support
    #SupportMigration


  • 10.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Thu September 17, 2020 07:25 AM

    hmm... works for me. I tested the url-open snippet with my FileCloud and got an ok response. Did you verify that you have a connectivity from your DP to FileCloud? The complete working sample below:


    var urlopen = require('urlopen');


    var options = {

          target: 'https://MYTEAM.filecloudonline.com/core/loginguest',

          method: 'post',

        contentType: 'application/x-www-form-urlencoded',

          timeout: 60,

     sslClientProfile: 'default-tls',

           data: "password=MYPASSWORD&userid=MYUSERNAME"};


    urlopen.open(options, function(error, response) {

     if (error) {

      // an error occurred during the request sending or response header parsing

      session.output.write("urlopen error: "+JSON.stringify(error));

     } else {

      // get the response status code

      var responseStatusCode = response.statusCode;

      var responseReasonPhrase = response.reasonPhrase;

      console.log("Response status code: " + responseStatusCode);

      console.log("Response reason phrase: " + responseReasonPhrase);

      // reading response data

      response.readAsBuffer(function(error, responseData){

       if (error){

        throw error ;

       } else {

        session.output.write(responseData) ;

       }

      });

     }

    });



    #DataPower
    #Support
    #SupportMigration


  • 11.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Thu September 17, 2020 12:34 PM

    Hi Hermanni,


    Thanks a lot for your creating code to call the exactly same target FileCloud. I tested tcp-conn from my DataPower to the FileCloud at port 443 and 80. They all work. I shall try your code later after my 2 meetings in the morning. I shall get back to you with my results.


    Thanks,


    Tang



    #DataPower
    #Support
    #SupportMigration


  • 12.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Tue September 29, 2020 08:59 PM

    Hi Hermanni,


    Sorry, I was pulled away for work with higher priority.


    Today I tried your code and I still got "The header is empty when connecting to URL " error for url-open call. Can you have a look at it?


    1. I created a DataPower gateway between my postman client and FileCloud. The flow is postman ---https--->DataPower gateway---https-->FileCloud. I used postman to test and it works.
    2. But when I added a gatewayscript action to call FileCloud using url-open in the above DataPower gateway. I used same postman to test. I see url-open failed with error "The header is empty when connecting to URL "


    I don't know how to attach DataPower gateway, package capture, and error report here. If you let me know your email, I shall send them to you. but my gateway script is like below


    var urlopen = require ('urlopen');


    var options = {  

     target: 'https://doisftp.filecloudonline.com:443/core/loginguest',

     method: 'post',

     headers: {

      'Content-Type': 'application/x-www-form-urlencoded',

    'User-Agent': 'Mozilla/5.0',

    Accept: 'text/plain', 

    'Accept-Charset': 'utf-8',

    'Accept-Encoding': 'gzip,deflate,br',

    'Connection': 'keep-alive',

      'Cookie': 'tonido-login-user=victor.reyesviloria; X-XSRF-TOKEN=t1ds0lqchfmtjwhq1jhe; tonido-login-seed=d97b61b2-ec18-4f97-8fee-0b6afd52d350; tonido-login-hash=5d01574b4e09500969013196ed1029bfde4158c6; X-XSRF-TOKEN-user=sswoeg10prs0bwxdnnur'

     },

     data: "password=pwd123&userid=victor.smith"

    };


    // open connection to target and send data over

    urlopen.open(options, function (error, response) {

      console.error("start calling url-open");

    if (error) {

      console.error("start calling url-open failed.");

    // an error occurred during request sending or response header parsing

    session.output.write ("urlopen connect error: " + JSON.stringify(error));

    } else {

    // read response data

    // get the response status code

    var responseStatusCode = response.statusCode;

    console.error("responseStatusCode=" + responseStatusCode);

    if (responseStatusCode == 200) {

    response.readAsBuffer(function(error, responseData) {

    if (error) {

    // error while reading response or transferring data to Buffer

    session.output.write("readAsBuffer error: " + JSON.stringify(error));

    } else {

      console.error("responseData=" + responseData);

    session.output.write(responseData);

    });

    } else {

    session.output.write ("urlopen target return statusCode " + responseStatusCode);

    }

    }

    }); // end of urlopen.open()



    ////////////////////////////////////////////

    Note:

    1. as the userid and pwd are client, I changed to a fake one
    2. Some headers are from Postman node.js code for good testing the gateway with url-open call added.


    Thanks,


    Tang



    #DataPower
    #Support
    #SupportMigration


  • 13.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Wed September 30, 2020 12:35 PM

    I tested your code and it seems to work just fine. I can check your MPGW but if it doesn't reveal anything, I'd suggest that you'll open a PMR. You can send the MPGW export to hermanni.pernaa


    --Hermanni



    #DataPower
    #Support
    #SupportMigration


  • 14.  RE: How can I post parameters and add parameters in gatewayscript urlopen?

    Posted Fri October 02, 2020 04:47 PM

    Hi Hermanni,


    The issue 'header is empty' is fixed by using

    'User-Agent': 'PostmanRuntime/7.26.5',

    instead of 'User-Agent': 'Mozilla/5.0'.


    Thanks a lot for your help.


    Tang



    #DataPower
    #Support
    #SupportMigration