Handling x-www-form-urlencoded parameters IBM API Connect
Here in this blog, we will learn about handling x-www-form-urlencoded parameters IBM API Connect.
What are x-www-form-urlencoded parameters?
x-www-form-urlencoded parameters refer to a way of encoding form data before it is transmitted over HTTP. When a form is submitted from a web page, the data entered into the form fields needs to be sent to the server for processing. This data can be encoded in various formats, and one of the standard formats used is x-www-form-urlencoded.
Why do we need x-www-form-urlencoded parameters in IBM API Connect?
In IBM API Connect, the use of x-www-form-urlencoded parameters is often necessary for several reasons:
- Compatibility with Form Data: This format is commonly used to submit data from HTML forms. Many APIs are designed to receive data in this format, making it a widely supported standard for sending data over HTTP.
- Simple Key-Value Pairs: x-www-form-urlencoded parameters are structured as key-value pairs separated by ampersands (&). This simplicity makes it easy to understand and work with, especially when dealing with relatively straightforward data structures.
- Security and Encoding: This format automatically encodes special characters such as spaces or non-alphanumeric characters, ensuring that the data is transmitted safely over the network. This helps prevent issues like data corruption or injection attacks.
- Client-Side Handling: Many client-side libraries and frameworks, such as gatewayScript’s FormData object, natively support x-www-form-urlencoded formatting. This makes it convenient for developers working with web applications and APIs to handle and manipulate form data.
- API Gateway Compatibility: API gateways, including IBM API Connect, often expect incoming data in an x-www-form-urlencoded format for specific endpoints or operations. Adhering to this format ensures seamless integration with the gateway and avoids potential parsing or validation errors.
Step 1: Log in to API Connect.
Step 2: Go to the Design Tab >> Paths. Provide the name of the paths. And save the API.
Paths1 name:jsontoQueryString
Paths2 name: QueryStringtoJson
Step 3: Go to the Design Tab >> Paths>> Paths1. Configure the parameters of path1 and path2.
Click on save.
Step 4: Go to the Gateway Tab and select Policies.
Apply the logic of the operation switch. Include the cases that we set up in the section on pathways.
Place the gatewayscript policies in the assembly flow by dragging them there. And save the API.
x-www-form-urlencoded, where key-value pairs are formatted as key=value and separated by ampersands (&).
Ex:
name=John Doe&age=30&city=New York
In this case, name, age, and city are the field names, and John Doe, 30, and New York are the corresponding values. Each key-value pair is URL-encoded, meaning that special characters are replaced with their percent-encoded representation. Spaces are typically encoded as + or %20.
The resulting encoded data will be:
name=John%20Doe&age=30&city=New%20York
Note: To fulfill the criteria, utilize the gatewayscript policy and write the code.
API Path1: JSON to queryString
It can handle the JSON request and convert it into x-www-form-urlencoded parameters as a response.
Method: POST
Providing input in the format of JSON and Providing “application/x-www-form-urlencoded” content type is the output.
API Path2: queryString to JSON
It can handle the x-www-form-urlencoded request and convert it into a JSON Response.
Method: POST
Providing input in the format of x-www-form-urlencoded and the “application/json” content type is the output.
Step 5: Test the API with any testing toolkit.
PFB screenshot for testing of queryString to JSON
PFB screenshot for testing of JSON to queryString