swifthttpalamofirecrud

why Alamofire parameter brackets encoding?


While using Alamofire

There was a problem while delivering parameters using GET!

Can someone help me?

   
     let url = "\(urlAIString)/Url-Space/position=[\(userLocation)]"
     AF.request(url,method: .get){
            ....
     }

I want the brackets to be delivered to the server without being encoded. However, I am experiencing inconvenience because the brackets are encoded in base64 and delivered


Solution

  • I want the brackets to be delivered to the server without being encoded.

    This is not a URL, and so the protocol you're using is not HTTP. Going back to RFC 1738, the spec is clear:

    Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

    While you (and the service your working with) are free to define some non-HTTP protocol that looks a lot like HTTP, but also supports GET-like requests that are not URLs, that's not a protocol URLSession (which Almofire relies on) supports. It will be up to you to build your own networking transport to interact with this service. Generally you would need to build that on top of something like the Network framework. This is generally a bit of work, so it is usually easier to make your service use a standard protocol like HTTP and properly-encoded URLs, rather than inventing a custom protocol like the one you describe.

    If you do build a custom protocol to talk to this service, look at NWProtocolTLS. If your service is very similar to HTTP/1.0, and you don't use any advanced features, it isn't that difficult to build it from scratch on top of a TLS connection. (TLS is a lot of work to implement, so let the system do that work for you.) Simple HTTP can be written and parsed by hand.

    Integrating this into Alamofire would require a URLProtocol to take over the transport layer. This isn't a ton of code, but is somewhat complex to set up. If you build a network service for this, and have trouble with URLProtocol, open a more specific question on that when you get there.

    I recommend fixing your server instead. It's usually easier.