In Azure API Management, I need to add values to form-data body and pass to back end

Robert Heynen 50 Reputation points
2023-12-28T04:17:13.6066667+00:00

Is it possible to add some Keys/Values to a form-data body and then pass that body to the backend URL.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,826 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Pramod Valavala 20,596 Reputation points Microsoft Employee
    2024-01-03T16:44:19.4566667+00:00

    @Robert Heynen Form Data can be extracted in the set-body policy as mentioned in the official docs and you should be able to append values to it like so.

    <set-body template="none">@{
    var formData = context.Request.Body.AsFormUrlEncodedContent();
    
    formData.Add("secret", new List<string>() {"123", "456"});
    
    return formData.ToFormUrlEncodedContent();
    }</set-body>
    

    If your backend requires JSON, you can replace the return line with JsonConvert.SerializeObject(formData) which will output the object as JSON instead.

    1 person found this answer helpful.

  2. Pinaki Ghatak 2,405 Reputation points Microsoft Employee
    2023-12-28T05:15:00.3333333+00:00

    Your best and easiest option is to use the form data, make a json object, add additional property to the JSON body and put that in the request body for a POST message to the API backend.