Search code examples
swifthttphttp-headershttprequesthttp-parameters

What is the difference between HTTP parameters and HTTP headers?


I read this question but it didn't answer my question.

To me Headers and Parameters are both dictionaries with the difference that headers is [String : String] while Parameters is [String : AnyObject]? and so if your parameters are also Strings then you could send them within the headers (while using a 'x-' prefix to signify they aren't standard headers) which is a common but not good practice.

  • Is that correct?
  • Are there other difference between headers and parameters?
  • What kind of other non-String types would you be sending using parameters?

Alamofire Request method

public func request(
        method: Method,
        _ URLString: URLStringConvertible,
          parameters: [String: AnyObject]? = nil,
          encoding: ParameterEncoding = .URL,
          headers: [String: String]? = nil)
        -> Request
    {
        return Manager.sharedInstance.request(
            method,
            URLString,
            parameters: parameters,
            encoding: encoding,
            headers: headers
        )
    }

As an example I have seen people passing ["x-ios-version" : UIDevice.currentDevice().systemVersion] or build versions through headers


Solution

  • Here is the list of differences:

    1. They are designed for different purposes. Headers carry meta info, parameters carry actual data.

    2. HTTP Servers will automatically un-escape/decode parameter names/values. This does not apply to header names/values.

    3. Header names/values need to be manually escaped/encoded at client side and be manually un-escaped/decoded at server side. Base64 encoding or percent escape is often used.

    4. Parameters can be seen by end-users (in URL), but headers are hidden to end-users.