Electrode-iOS / ELWebService

A lightweight HTTP networking framework for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add API for setting URL and body parameters separately

angelodipaolo opened this issue · comments

The current implementation does not conveniently support setting URL query parameters separately from request body parameters. This is because of API behavior where request parameters are set in the URL for GET and DELETE requests but are set in the body for all other HTTP methods. There are cases where you need to set the URL query parameters and body data independently from the HTTP method-based behavior.

This behavior was implemented to follow RESTful HTTP client conventions but I think deprecating this behavior and adding separate URL and body parameter methods makes the API more flexible and makes the intent of the code much clearer.

I suggest the following changes:

  1. Deprecate setParameters(parameters:encoding:) and setParameterEncoding(encoding) methods. The API would be removed in the next major release.
  2. Add methods that enable setting URL query parameters and request body parameters separately.

Add setQueryParameters() method for setting URL query parameters.

This new method would enable your code to set the URL query parameters seperately from the request body data.

service
    .POST("/brewers")
    .setQueryParameters(["included-fields": "all"]) // set URL query params
    .setJSON(["name": "Trashboat Brewing"])         // set JSON body

The resulting HTTP request would look like:

POST /brewers?included-fields=all HTTP/1.1
Content-Type: application/json

{"name": "Trashboat Brewing"}

Add setPercentEncodedBodyParameters() method for setting parameters as percent-encoded data in the request body.

service
    .POST("/brewers")
    .setQueryParameters(["included-fields": "all"])                 // set URL params
    .setPercentEncodedBodyParameters(["name": "Trashboat Brewing"]) // set body params

The resulting HTTP request would look like:

POST /brewers?included-fields=all HTTP/1.1
Content-Length: 55

name=Trashboat%20Brewing

I just had the thought that setFormData() might be a good name for the setPercentEncodedBodyParameters() method. It would match up well with setJSON(), and the fact that the content type will be application/x-www-form-urlencoded.

Great point. I agree a name like setFormData() is more appropriate considering that the method will also need to set the content-type to application/x-www-form-urlencoded.