In order to make HTTP request call using URLSession,I need to convert string to URL first so I have tried everything but just can’t convert this piece of string to URL(string: ).
This is the string:
“http://api-aws-eu-qa-1.abc-cde.com/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”
I can’t share the exact Url but format is all same.
The actual url with same format works fine in browser but no way in Swift, also that works fine in POSTMAN
I have also tried URLComponents which hepls me create the URL from components to URL but that fails with 400 status code response error.
I really appreciate the help, I am completely bogged down with this isuue and can’t proceed with my assignment.
Update: Tested with fiddler and request was not going through with this URL – "“http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”"
But when removed this %E2%80%8B in fiddler and resend it worked.
Any thoughts ..?
3
Answers
try to add HEADER to you request:
String to URL
URL to String
Creating URL from URLComponents
Your URL is:
The "host" portion is:
%E2%80%8B
encodes a ZERO WIDTH SPACE. This is a perfectly valid URL:However, there is no such host as
api-aws-eu-qa-1.abc-cde.com%E2%80%8B
, so you should expect the actual connection to fail.I expect that whatever is generating your URL strings has a bug.
If you are having trouble creating the URL itself (if
URL(string:)
return nil, then I expect this is not your actual URL string.If you construct an URLComponents, the results are correct but may be slightly misleading:
While this looks like
"api-aws-eu-qa-1.abc-cde.com"
, the string actually has a ZERO WIDTH SPACE at the end, which is invisible (but still part of the host field, which will correctly fail if you try to connect to it).