Layer7 API Management

 View Only
  • 1.  change value of JSON key based on specific request header

    Posted Aug 06, 2019 08:59 PM
    I have a specific requirement wherein I need to pass on a json message as the request body. This json message has a whole bunch of key:value pairs, of which most values would remain the same for all the requests. We want the end user to have the ability to customize specific fields, which would change the value of those specific fields in the json message.

    I am thinking of creating a context variable with the default json message as the value, and to have a whole bunch of request headers (for the user customization part), and to read those header values once the user submits the request.
    Is there an easier way to replace the value of specific fields within the json message with the values from the request headers.


  • 2.  RE: change value of JSON key based on specific request header

    Broadcom Employee
    Posted Aug 07, 2019 08:42 AM
    Hi Maddy,

    Can you use a form post with the custom values? This may save some overhead of loading up a bunch of HTTP headers.

    ie: community=layer7&name=Maddy&customName=customVal

    We could the retrieve these via a built-in context variable ${request.http.paramater.<name>} as use RegEx to replace the values in the JSON body as you noted with the variables.

    Regards,
    Joe


  • 3.  RE: change value of JSON key based on specific request header

    Posted Aug 07, 2019 01:37 PM
    Thanks Joe.
    Is it possible to use RegEx to search a specific field in json body and replace the value for that field (I have used RegEx before for strings, but not on json). Can you please provide a sample to extract a specific field within a json message using RegEx.

    Also with the above one, do I have to use RegEx for each and every header value, for replacing the value in json body.


  • 4.  RE: change value of JSON key based on specific request header
    Best Answer

    Broadcom Employee
    Posted Aug 08, 2019 09:50 AM
    With Regex you can use the match and replace feature if you know the key name, ie:

    Sample JSON: 
    {
     "name": "layer7",
     "dept": "support",
     "custom": "value1",
     "custom2": "value2"
    }


    I believe this would need to be run against every value you wish to replace as I am not familiar with a RegEx pattern that would be able to handle this in a single statement.