Skip to content

Instantly share code, notes, and snippets.

@and1truong
Created January 9, 2025 04:05
Show Gist options
  • Save and1truong/d78628970ca895b6e9f49f92fa893c3b to your computer and use it in GitHub Desktop.
Save and1truong/d78628970ca895b6e9f49f92fa893c3b to your computer and use it in GitHub Desktop.
What is the best practice for passing an array of strings as query parameters in an HTTP request, and how can it be effectively parsed on the server side?

Best Practices for Passing Arrays of Strings as Query Parameters:

  • URL Encoding:

    • Individual Encoding: Each string within the array should be URL encoded individually. This ensures proper handling of special characters like spaces, ampersands, and other reserved characters.
    • Example:
      • my_array[]=value1&my_array[]=value2&my_array[]=value3
  • Key-Value Pairs:

    • Repeated Keys: Use the same key for each value in the array, but with an empty index or a sequential index.
    • Example:
      • my_array[]=value1&my_array[]=value2&my_array[]=value3
      • my_array[0]=value1&my_array[1]=value2&my_array[2]=value3
  • Comma-Separated Values (CSV):

    • Single Key: Use a single key and concatenate the array values with commas.
    • Example:
      • my_array=value1,value2,value3
    • Note: This method can be less robust if array values may themselves contain commas.

Server-Side Parsing:

  • Language-Specific Libraries:

    • Python (Flask/Django):
      • Use request.args.getlist('my_array') to retrieve the list of values.
    • Node.js (Express):
      • Use req.query.my_array to access the array of values.
    • Java (Spring):
      • Use @RequestParam(value = "my_array", required = false) and specify List<String> as the parameter type.
    • PHP:
      • Use $_GET['my_array'] to access the array of values. If using the comma-separated format, use explode(',', $_GET['my_array']) to split the string.
  • Manual Parsing:

    • Split by Delimiter: If using comma-separated values, split the string by the delimiter (e.g., comma).
    • Handle Special Cases: Consider edge cases like empty values or values containing the delimiter character.

Example (Python with Flask):

from flask import Flask, request

app = Flask(__name__)

@app.route('/my_endpoint')
def my_endpoint():
    array_values = request.args.getlist('my_array') 
    print(array_values) 
    return 'Array values:', array_values

if __name__ == '__main__':
    app.run(debug=True)

Choosing the Best Method:

  • Flexibility and Maintainability: Key-value pairs with repeated keys generally offer the most flexibility and are often easier to maintain.
  • Simplicity: Comma-separated values can be simpler for basic use cases.
  • Server-Side Considerations: Choose a method that aligns with your server-side framework and libraries for easier parsing.

Important Considerations:

  • URL Length Limits: Be mindful of URL length limitations. Long arrays of strings can lead to excessively long URLs. Consider alternative approaches like using a request body for larger arrays.
  • Security: Always properly sanitize and validate user input to prevent security vulnerabilities like injection attacks.

By following these best practices, you can effectively pass arrays of strings as query parameters in your HTTP requests and efficiently parse them on the server-side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment