API Connect

 View Only
  • 1.  Content-type application/x-www-form-urlencoded

    Posted Fri April 05, 2024 06:29 AM

    Hi,

    I am consuming an api which content-type is x-www-form-urlencoded and i have check their is no way to define x-www-form-urlencoded 
    kindly tell me how can i used this type of api.



    ------------------------------
    Shah Wajahat
    ------------------------------


  • 2.  RE: Content-type application/x-www-form-urlencoded

    Posted Sun April 07, 2024 03:28 AM

    Hi Wajahat,

    Here is a link to already suggested solutions:

    https://community.ibm.com/community/user/integration/discussion/how-to-invoke-target-url-api-with-x-www-form-urlencoded-parameters?fireglass_rsn=true#bm460389ba-c87e-4ed5-92a7-aaf338901a04



    ------------------------------
    Sergey Paponov
    ------------------------------



  • 3.  RE: Content-type application/x-www-form-urlencoded

    Posted Wed April 10, 2024 05:31 PM

    Hi Shah,
    Are you referring to the API UI consumes property? It has a dropdown for appliance/xml and application/json, but you can also specify your x-www-form-urlencoded in that combo entry field.  The post from a few years back also referenced was to create a form based post to a backend in GatewayScript. Once you consume that query string like post, if you need to look at it you'll either need to parse it, which will parse it as a binary document, or you can specify buffering: true so the payload will be fully read before the assembly begins.
    Regards,
    Steve



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



  • 4.  RE: Content-type application/x-www-form-urlencoded

    Posted Sun April 14, 2024 08:07 AM

    Hi Steve 

    Please their is an example xml or any other references 



    ------------------------------
    Shah Wajahat
    ------------------------------



  • 5.  RE: Content-type application/x-www-form-urlencoded

    Posted Mon April 15, 2024 10:02 AM

    Hi steve:

    I am using message body as a json and write gatewayscript 

    but i am unable sent data to backend.

     kindly help me



    ------------------------------
    Shah Wajahat
    ------------------------------



  • 6.  RE: Content-type application/x-www-form-urlencoded

    Posted Sun April 21, 2024 09:26 AM

    Hello Shah.

    I've just implemented 'url-form-encoded' request for endpoint.

    This was not clear for me too. One of the unexpected and undescribed tricks has in 'invoke' !

    Here my suggested solution for you (hope this will useful):

    (Here is my invoke preparation in gateway script. You can pay attention that I'm not use 'apim' in v10 )

    // let's assume your request body have two params 
    var requestBody = {
        'param1': "some_param",
        'param2': "another_param"
    };
    
    // building new request form body by iteration through all parameters in requst body
    var formBody = [];
    for (var property in requestBody){
        var encodedKey = encodeURIComponent(property);
        var encodedValue = encodeURIComponent(properties[property]);
        formBody.push(encodedKey + '=' + encodedValue);
    }
    // join(concate) all  parameters by necessary 'ampersand' symbol  
    formBody = formBody.join('&');
    //set necessary header 
    context.message.header.set('Content-Type','application/x-www-form-urlencoded');
    //write body for for context for new invoke
    context.message.body.write(formBody);

    (And here is invoke defitions):

    - invoke:
              version: 2.2.0
              title: invoke
              backend-type: detect
              header-control:
                type: blocklist
                values: []
              parameter-control:
                type: allowlist
                values: []
              http-version: HTTP/1.1
              timeout: 60
              verb: keep
              chunked-uploads: false
              persistent-connection: true
              cache-response: protocol
              stop-on-error: []
              websocket-upgrade: false
              target-url: $(backendUrl)
              inject-proxy-headers: false
              decode-request-params: false
              encode-plus-char: true
              graphql-send-type: detect

    Thanks,

    Sergey



    ------------------------------
    Sergey Paponov
    ------------------------------



  • 7.  RE: Content-type application/x-www-form-urlencoded

    Posted Tue April 30, 2024 01:28 PM

    Hi Shah,
    Looking at your GatewayScript, you are POSTing a string that looks like input_data: <request.body>. Form posts should look like a query string except they are in the posted payload, ie, something like param1=value1&param2=value2...&paramn=valuen so if your backend is really expecting an application/x-www-form-urlencoded payload it doesn't understand what you are sending.  Sergev's example is essentially taking every property from an application/json request.body payload and creating this & separated string of name=value pairs.  The invoke policy will post whatever is in message.body and will provide to the backend whatever is in message.headers as the request headers to the backend server.
    Regards,

    Steve



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