How to prevent URLComponents().port from adding a question mark before the query (Swift/Xcode)












-1















I am trying to compose a representative URLComponents() in the app I am designing.



Here is the code:



class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

var components = URLComponents()

components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
components.queryItems = [queryItemToken]

print(components.url as Any)
}
}


Here is the output of the above snippet:



Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)


The above output doesn't work on the server because of the ? between the port and the query!
How can I prevent URLComponents() from inserting this redundant ? between the port and the query!



The target output: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)










share|improve this question




















  • 2





    It isn't "redundant". That is what a query is.

    – matt
    Jan 2 at 22:41
















-1















I am trying to compose a representative URLComponents() in the app I am designing.



Here is the code:



class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

var components = URLComponents()

components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
components.queryItems = [queryItemToken]

print(components.url as Any)
}
}


Here is the output of the above snippet:



Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)


The above output doesn't work on the server because of the ? between the port and the query!
How can I prevent URLComponents() from inserting this redundant ? between the port and the query!



The target output: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)










share|improve this question




















  • 2





    It isn't "redundant". That is what a query is.

    – matt
    Jan 2 at 22:41














-1












-1








-1








I am trying to compose a representative URLComponents() in the app I am designing.



Here is the code:



class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

var components = URLComponents()

components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
components.queryItems = [queryItemToken]

print(components.url as Any)
}
}


Here is the output of the above snippet:



Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)


The above output doesn't work on the server because of the ? between the port and the query!
How can I prevent URLComponents() from inserting this redundant ? between the port and the query!



The target output: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)










share|improve this question
















I am trying to compose a representative URLComponents() in the app I am designing.



Here is the code:



class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

var components = URLComponents()

components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
components.queryItems = [queryItemToken]

print(components.url as Any)
}
}


Here is the output of the above snippet:



Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)


The above output doesn't work on the server because of the ? between the port and the query!
How can I prevent URLComponents() from inserting this redundant ? between the port and the query!



The target output: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)







swift nsurlcomponents






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 1:22









Cœur

19.2k9115155




19.2k9115155










asked Jan 2 at 22:29









mansantomansanto

12612




12612








  • 2





    It isn't "redundant". That is what a query is.

    – matt
    Jan 2 at 22:41














  • 2





    It isn't "redundant". That is what a query is.

    – matt
    Jan 2 at 22:41








2




2





It isn't "redundant". That is what a query is.

– matt
Jan 2 at 22:41





It isn't "redundant". That is what a query is.

– matt
Jan 2 at 22:41












2 Answers
2






active

oldest

votes


















3














The /predict part is the path, not a query item. text is the actual query parameter.



You want:



var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
components.path = "/predict"
let queryItemToken = URLQueryItem(name: "text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url!)





share|improve this answer


























  • thank you so much, this took care of it.

    – mansanto
    Jan 2 at 22:43











  • @maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

    – Craig
    Jan 2 at 22:44











  • The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

    – rmaddy
    Jan 2 at 22:57











  • I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

    – Craig
    Jan 2 at 23:01













  • Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

    – rmaddy
    Jan 2 at 23:03



















-2














Thank you all for the response. I got away with all this by doing the following without the need to use URLComponents().



It turned out that sending some raw special characters in the query can be devastating to the request to the network.



And then, I use a dictionary to replace some special characters in the raw input before processing further, everything else works smooth. Thanks a lot for the attention.



So, assuming the user raw input is input:



import UIKit
import Foundation

// An example of a user input
var input = "what's your name?"

// ASCII Encoding Reference: important to allow primary communication with the server
var mods = ["'": "%27",
"’": "%27",
" ": "%20",
""" : "%22",
"<" : "%3C",
">" : "%3E"]

for (spChar, repl) in mods {
input = input.replacingOccurrences(of: spChar, with: repl, options: .literal, range: nil)
}

let query = "http://0.0.0.0:9090/predict?text=" + input


This is my third day with swift, I am sure there must be cleaner approaches to handle these nuances.






share|improve this answer



















  • 1





    If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

    – Sulthan
    Jan 3 at 14:16











  • This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

    – Rob Napier
    Jan 3 at 15:53











  • "I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

    – rmaddy
    Jan 3 at 16:26











  • I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

    – mansanto
    Jan 4 at 5:21













  • @mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

    – rmaddy
    Jan 4 at 5:47












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014004%2fhow-to-prevent-urlcomponents-port-from-adding-a-question-mark-before-the-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The /predict part is the path, not a query item. text is the actual query parameter.



You want:



var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
components.path = "/predict"
let queryItemToken = URLQueryItem(name: "text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url!)





share|improve this answer


























  • thank you so much, this took care of it.

    – mansanto
    Jan 2 at 22:43











  • @maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

    – Craig
    Jan 2 at 22:44











  • The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

    – rmaddy
    Jan 2 at 22:57











  • I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

    – Craig
    Jan 2 at 23:01













  • Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

    – rmaddy
    Jan 2 at 23:03
















3














The /predict part is the path, not a query item. text is the actual query parameter.



You want:



var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
components.path = "/predict"
let queryItemToken = URLQueryItem(name: "text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url!)





share|improve this answer


























  • thank you so much, this took care of it.

    – mansanto
    Jan 2 at 22:43











  • @maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

    – Craig
    Jan 2 at 22:44











  • The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

    – rmaddy
    Jan 2 at 22:57











  • I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

    – Craig
    Jan 2 at 23:01













  • Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

    – rmaddy
    Jan 2 at 23:03














3












3








3







The /predict part is the path, not a query item. text is the actual query parameter.



You want:



var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
components.path = "/predict"
let queryItemToken = URLQueryItem(name: "text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url!)





share|improve this answer















The /predict part is the path, not a query item. text is the actual query parameter.



You want:



var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
components.path = "/predict"
let queryItemToken = URLQueryItem(name: "text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url!)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 22:42

























answered Jan 2 at 22:36









rmaddyrmaddy

246k27327390




246k27327390













  • thank you so much, this took care of it.

    – mansanto
    Jan 2 at 22:43











  • @maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

    – Craig
    Jan 2 at 22:44











  • The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

    – rmaddy
    Jan 2 at 22:57











  • I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

    – Craig
    Jan 2 at 23:01













  • Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

    – rmaddy
    Jan 2 at 23:03



















  • thank you so much, this took care of it.

    – mansanto
    Jan 2 at 22:43











  • @maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

    – Craig
    Jan 2 at 22:44











  • The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

    – rmaddy
    Jan 2 at 22:57











  • I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

    – Craig
    Jan 2 at 23:01













  • Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

    – rmaddy
    Jan 2 at 23:03

















thank you so much, this took care of it.

– mansanto
Jan 2 at 22:43





thank you so much, this took care of it.

– mansanto
Jan 2 at 22:43













@maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

– Craig
Jan 2 at 22:44





@maddy - you're so quick with these answer, small thing though doesn't the print(components.url!) force unwrap a nil?

– Craig
Jan 2 at 22:44













The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

– rmaddy
Jan 2 at 22:57





The ! force unwraps an optional, not a nil. If it was nil it would have crashed. It would be nil if your components didn't create a valid URL. It's just a print statement for debugging purposes so it doesn't matter if it crashes during development. But certainly do not leave it there.

– rmaddy
Jan 2 at 22:57













I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

– Craig
Jan 2 at 23:01







I found the problem in my Playground I'd left off the leading / when setting the path… components.path = "predict" fails

– Craig
Jan 2 at 23:01















Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

– rmaddy
Jan 2 at 23:03





Yes, it would. That's why I set the path to "/predict" and not "predict" in my answer.

– rmaddy
Jan 2 at 23:03













-2














Thank you all for the response. I got away with all this by doing the following without the need to use URLComponents().



It turned out that sending some raw special characters in the query can be devastating to the request to the network.



And then, I use a dictionary to replace some special characters in the raw input before processing further, everything else works smooth. Thanks a lot for the attention.



So, assuming the user raw input is input:



import UIKit
import Foundation

// An example of a user input
var input = "what's your name?"

// ASCII Encoding Reference: important to allow primary communication with the server
var mods = ["'": "%27",
"’": "%27",
" ": "%20",
""" : "%22",
"<" : "%3C",
">" : "%3E"]

for (spChar, repl) in mods {
input = input.replacingOccurrences(of: spChar, with: repl, options: .literal, range: nil)
}

let query = "http://0.0.0.0:9090/predict?text=" + input


This is my third day with swift, I am sure there must be cleaner approaches to handle these nuances.






share|improve this answer



















  • 1





    If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

    – Sulthan
    Jan 3 at 14:16











  • This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

    – Rob Napier
    Jan 3 at 15:53











  • "I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

    – rmaddy
    Jan 3 at 16:26











  • I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

    – mansanto
    Jan 4 at 5:21













  • @mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

    – rmaddy
    Jan 4 at 5:47
















-2














Thank you all for the response. I got away with all this by doing the following without the need to use URLComponents().



It turned out that sending some raw special characters in the query can be devastating to the request to the network.



And then, I use a dictionary to replace some special characters in the raw input before processing further, everything else works smooth. Thanks a lot for the attention.



So, assuming the user raw input is input:



import UIKit
import Foundation

// An example of a user input
var input = "what's your name?"

// ASCII Encoding Reference: important to allow primary communication with the server
var mods = ["'": "%27",
"’": "%27",
" ": "%20",
""" : "%22",
"<" : "%3C",
">" : "%3E"]

for (spChar, repl) in mods {
input = input.replacingOccurrences(of: spChar, with: repl, options: .literal, range: nil)
}

let query = "http://0.0.0.0:9090/predict?text=" + input


This is my third day with swift, I am sure there must be cleaner approaches to handle these nuances.






share|improve this answer



















  • 1





    If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

    – Sulthan
    Jan 3 at 14:16











  • This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

    – Rob Napier
    Jan 3 at 15:53











  • "I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

    – rmaddy
    Jan 3 at 16:26











  • I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

    – mansanto
    Jan 4 at 5:21













  • @mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

    – rmaddy
    Jan 4 at 5:47














-2












-2








-2







Thank you all for the response. I got away with all this by doing the following without the need to use URLComponents().



It turned out that sending some raw special characters in the query can be devastating to the request to the network.



And then, I use a dictionary to replace some special characters in the raw input before processing further, everything else works smooth. Thanks a lot for the attention.



So, assuming the user raw input is input:



import UIKit
import Foundation

// An example of a user input
var input = "what's your name?"

// ASCII Encoding Reference: important to allow primary communication with the server
var mods = ["'": "%27",
"’": "%27",
" ": "%20",
""" : "%22",
"<" : "%3C",
">" : "%3E"]

for (spChar, repl) in mods {
input = input.replacingOccurrences(of: spChar, with: repl, options: .literal, range: nil)
}

let query = "http://0.0.0.0:9090/predict?text=" + input


This is my third day with swift, I am sure there must be cleaner approaches to handle these nuances.






share|improve this answer













Thank you all for the response. I got away with all this by doing the following without the need to use URLComponents().



It turned out that sending some raw special characters in the query can be devastating to the request to the network.



And then, I use a dictionary to replace some special characters in the raw input before processing further, everything else works smooth. Thanks a lot for the attention.



So, assuming the user raw input is input:



import UIKit
import Foundation

// An example of a user input
var input = "what's your name?"

// ASCII Encoding Reference: important to allow primary communication with the server
var mods = ["'": "%27",
"’": "%27",
" ": "%20",
""" : "%22",
"<" : "%3C",
">" : "%3E"]

for (spChar, repl) in mods {
input = input.replacingOccurrences(of: spChar, with: repl, options: .literal, range: nil)
}

let query = "http://0.0.0.0:9090/predict?text=" + input


This is my third day with swift, I am sure there must be cleaner approaches to handle these nuances.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 7:44









mansantomansanto

12612




12612








  • 1





    If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

    – Sulthan
    Jan 3 at 14:16











  • This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

    – Rob Napier
    Jan 3 at 15:53











  • "I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

    – rmaddy
    Jan 3 at 16:26











  • I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

    – mansanto
    Jan 4 at 5:21













  • @mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

    – rmaddy
    Jan 4 at 5:47














  • 1





    If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

    – Sulthan
    Jan 3 at 14:16











  • This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

    – Rob Napier
    Jan 3 at 15:53











  • "I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

    – rmaddy
    Jan 3 at 16:26











  • I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

    – mansanto
    Jan 4 at 5:21













  • @mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

    – rmaddy
    Jan 4 at 5:47








1




1





If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

– Sulthan
Jan 3 at 14:16





If you don't want to use URLComponent then please search how to do "URL-encoding" in Swift correctly, e.g. stackoverflow.com/a/39767927/669586

– Sulthan
Jan 3 at 14:16













This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

– Rob Napier
Jan 3 at 15:53





This encoding is wrong. There are more special characters than these. Most importantly %. Do not try to hand-encode URLs. It's extremely complicated. Use URLComponents, which does it correctly for you.

– Rob Napier
Jan 3 at 15:53













"I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

– rmaddy
Jan 3 at 16:26





"I am sure there must be cleaner approaches to handle these nuances" - yes, use URLComponents.

– rmaddy
Jan 3 at 16:26













I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

– mansanto
Jan 4 at 5:21







I tried to use the URLComponents implementing rmaddy's correct answer to my posted issue and it didn't resolve these special characters related issues. For example, URLComponents never replaced the apostrophe in a user input of "what's your name?" with the %27 and that made all the difference in my specific case. I looked around and I couldn't find any method in the URLComponents class which would solve this specific problem and shields me from hand-coding.

– mansanto
Jan 4 at 5:21















@mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

– rmaddy
Jan 4 at 5:47





@mansanto The ' doesn't need to be escaped. It's not a special URL character. That's why URLComponents didn't escape it. And your question shows that your desired output doesn't escape the '.

– rmaddy
Jan 4 at 5:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014004%2fhow-to-prevent-urlcomponents-port-from-adding-a-question-mark-before-the-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter