In many cases it's necessary to send data with application/x-www-form-urlencoded content type, and it turns out encoding to this content type is a bit of a mess. Firstly, when it was originally defined in HTML 4.01 in 1999, it wasn't registered as an IANA media type. It has retroactively been added to the registry, but resulted in additional definitions such as this one in OAuth 2.0 which is used for the token endpoint auth method known as "client_secret_basic". It was also redefined in the WHATWG specs: https://url.spec.whatwg.org/#concept-urlencoded-serializer
It's also maybe used for Authorization: Basic <...> headers — though the OAuth 2.0 spec may be deviating from RFC7617 here as RFC6749 (OAuth 2.0) is older.
I thought initially that it should be String.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), however that didn't seem to be correct as it doesn't encode + to %2B. I think that may be for the entirety of a query string, not the individual parameter names and values.
In projects like AlamoFire, they have a thousand-ish line encoder for URLEncodedForm: https://github.com/Alamofire/Alamofire/blob/master/Source/Features/URLEncodedFormEncoder.swift
I'm not sure if it would make sense to go that far, but maybe we just need a CharacterSet that is correct for this encoding?
This is also the default content type for POST request bodies.
In many cases it's necessary to send data with
application/x-www-form-urlencodedcontent type, and it turns out encoding to this content type is a bit of a mess. Firstly, when it was originally defined in HTML 4.01 in 1999, it wasn't registered as an IANA media type. It has retroactively been added to the registry, but resulted in additional definitions such as this one in OAuth 2.0 which is used for the token endpoint auth method known as "client_secret_basic". It was also redefined in the WHATWG specs: https://url.spec.whatwg.org/#concept-urlencoded-serializerIt's also maybe used for
Authorization: Basic <...>headers — though the OAuth 2.0 spec may be deviating from RFC7617 here as RFC6749 (OAuth 2.0) is older.I thought initially that it should be
String.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), however that didn't seem to be correct as it doesn't encode+to%2B. I think that may be for the entirety of a query string, not the individual parameter names and values.In projects like AlamoFire, they have a thousand-ish line encoder for URLEncodedForm: https://github.com/Alamofire/Alamofire/blob/master/Source/Features/URLEncodedFormEncoder.swift
I'm not sure if it would make sense to go that far, but maybe we just need a CharacterSet that is correct for this encoding?
This is also the default content type for POST request bodies.