Layer7 API Management

  • 1.  How to fetch data from form Data ?

    Posted Dec 17, 2018 02:01 PM

    Hi All,

    Can you please suggest on how to fetch certain data from the form data submitted by user ?

    I came across Validate HTML Form Data assertion but not really suitable to use.



  • 2.  Re: How to fetch data from form Data ?

    Broadcom Employee
    Posted Dec 17, 2018 02:23 PM

    Hi it might be helpful to have a sample bit of data and what you are trying to extract. 
    But usually form-data is a multi-part message. And usually you can extract the contents of the multi-part by accessing the gateway parts variable.

     

    ${request.parts}

    Message Layer Context Variables - CA API Gateway - 9.3 - CA Technologies Documentation 

     

    But sometimes the request.partsx mainpart is null this typically is due to the Content-Type of the object. So you may need to look at the following,

    request.mainpart is null 

     

    Since this deals with an additional system.property AND a clusterwide property named,

    contentType.otherTextualTypes

     

    I hope this helps. 



  • 3.  Re: How to fetch data from form Data ?

    Posted Dec 18, 2018 06:01 AM

    Hi Charles, 

     

    Thanks for your response.

    I have a simple form that submits userid.

    "SenderDetails" :{

    "userId" : "ABC"

    }

     

    As you said , when the request is received, the content type is multipart/form-data. 

    ${request.parts} = {l} "com.ltech.common.mime.l@34243c332".

    So I did 'Request : Change content type to 'application/json'. This gives mainpart in a String format "--------------43547386234287Content-Disposition: form-data; name = "message"{"senderDetails":{"userId":"ABC"}}----------44528779264824--"

    I guess I just have to extract the userId now. 

    My queries are :

    1. Can I extract the info without converting the content type to application/json ?

    2. what is the long number that appears in mainpart?



  • 4.  Re: How to fetch data from form Data ?

    Posted Dec 18, 2018 06:09 AM

    3. How to fetch the userId in above example ?



  • 5.  Re: How to fetch data from form Data ?

    Broadcom Employee
    Posted Dec 18, 2018 08:52 AM

    Q1. Can I extract the info without converting the content type to application/json ?

    A1. You have already extracted it at this point. Are you trying to update it in the message or just get the value for use. 

    Simple way to store this part is just create a new context variable and have it contain the mainpart (assuming you just want to act on it within the policy and not update the multi-part message). For example contextvariable myNewMainpart

     

    Q2. what is the long number that appears in mainpart?

    A2. These are just the boundary ids if I am not mistaken that identify the message parts. They are common with multipart data. 

     

    Q3. How to fetch the userId in above example ?

    A3. Assuming you put this in myNewMainpart you should be able to encapsulate the ID with the Evaluate Regular Expression assertion OR Evaluate JSON PATH or any other number of items. 

    Though I would likely just apply a regular expression to 

    "userId":"ABC"

    Something like "userId":".+?"

    Then just split the variable on the : and use the right side content in the multipart context variable. 

     

    Hope that helps.


    Charlie.

     

     

     



  • 6.  Re: How to fetch data from form Data ?

    Posted Dec 18, 2018 09:28 AM

    Hi Charles, 

    Ignore pt 3.

    I had to convert the content type to application/json to get the data in a String format that I could use to fetch the userid from.

    The problem is that if I keep the content type to application/json and try to route to the backend service  url, then I get the internal server 500 error.

    However, if I disable the conversion to application/json and just directly hard code the user id to abc and route, it works perfectly with the hard coded uesrId.

    I tried to again change the value application/json to multipart/form-data with some boundary value = RFC2045, but it doesn't work that way.

     

    So isn't there any way to fetch the data from a POST request without converting the content-type ?



  • 7.  Re: How to fetch data from form Data ?

    Broadcom Employee
    Posted Jan 22, 2019 04:48 PM

    Good afternoon,

     

    I believe that the problem is that the post is not correct form data as it should look like:

    ----------------------------068736609991844823448248
    Content-Disposition: form-data; name="SenderDetails"

    {

    "userId" : "ABC"

    }
    ----------------------------068736609991844823448248--

     

    Due to this you will need to use the regular expression assertion with the expressions "userId" : "(.*)" to pull out the username.

     

    Additional information: java - REST - HTTP Post Multipart with JSON - Stack Overflow 

     

    Sincerely,

     

    Stephen Hughes

    Broadcom Support