0

I have a problem with backslash in strings.

When i create a string with this code:

 let url = webProtocol + siteHost + "/tables/coupon?$expand=source&$filter=Source/Name eq '" + category + "'&" + siteApi

I expect this URL

https://swoup.azurewebsites.net/tables/coupon?$expand=source&$filter=Source/Name eq 'Recommended'&ZUMO-API-VERSION=2.0.0

But I get this

https://swoup.azurewebsites.net/tables/coupon?$expand=source&$filter=Source/Name eq \'Recommended\'&?ZUMO-API-VERSION=2.0.0

I tried to delete them using

stringByReplacingOccurrencesOfString("\\", withString: "")

But it doesn't help. Also I tried to add backslash before the ' but it doesn't help.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • 6
    Why do you have spaces in the URL you expect? – Aaron Aug 30 '16 at 16:51
  • Have you encoded the url? – Nirav D Aug 30 '16 at 16:58
  • 2
    Supply a complete question,`webProtocol`, `siteHost`, `category` and`siteApi` are undefined. Provide a [mcve] with emphasis on complete. – zaph Aug 30 '16 at 17:03
  • As Aaron pointed out, the URL you are attempting to create is invalid, spaces and some other characters you use are invalid. Check here for valid characters in a URL -> http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid – Ogre Codes Aug 30 '16 at 17:08
  • Where/how do you you log the URL? Many tools display an escaped version and add backslashes. – jcaron Aug 30 '16 at 17:15

3 Answers3

1

It's not clear based on your question, but I believe the backslashes are not actually in the string, but being printed by XCode. For example, enter the following into a playground:

let siteApi="test=123"
let category="Category1"
let webProtocol="https://"
let siteHost="www.testme.com"
let url = webProtocol + siteHost + "/tables/coupon?$expand=source&$filter=Source/Name eq '" + category + "'&" + siteApi
print( url)

And you will see the output does not contain backslashes.

https://www.testme.com/tables/coupon?$expand=source&$filter=Source/Name eq 'Category1'&test=123
Ogre Codes
  • 18,693
  • 1
  • 17
  • 24
  • You aren't using back slashes, in his code category is being filled in with `\'` and `\'` is special code in strings to mean single apostrophe. The issue comes down do whatever is filling in category has to be doing `\\\'` – Knight0fDragon Aug 30 '16 at 17:10
  • It's super confusing. It's an incomplete example and mixing quotes makes it tough to read. Hopefully he'll post the full code and we can have a better idea of where the problem is. – Ogre Codes Aug 30 '16 at 17:14
  • indeed it is confusing – Knight0fDragon Aug 30 '16 at 17:14
  • Look at the placement of the backslashes, it's prior to the first single quote so I don't believe the issue is the contents of the category variable. – Ogre Codes Aug 30 '16 at 17:16
  • I thought that `'` alone is invalid in string literals and needed to be `\'` – Knight0fDragon Aug 30 '16 at 17:19
0

The error is in the variables that are not shown.

With this example complete (?) code example:

let webProtocol = "https://"
let siteHost = "swoup.azurewebsites.net"
let category = "Recommended"
let siteApi = "ZUMO-API-VERSION=2.0.0"
let url = webProtocol + siteHost + "/tables/coupon?$expand=source&$filter=Source/Name eq '" + category + "'&" + siteApi
print (url)

Output
https://swoup.azurewebsites.net/tables/coupon?$expand=source&$filter=Source/Name eq 'Recommended'&ZUMO-API-VERSION=2.0.0

There is no \.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

Use NSURLComponents and NSURLQueryItem to build URLs, not string concatenation.

let components = NSURLComponents()
components.scheme = "https"
components.host = "swoup.azurewebsites.net"
components.path = "/tables/coupon"
let category = "Recommended"
let expand = NSURLQueryItem(name: "expand", value: "source")
let filter = NSURLQueryItem(name: "filter", value: "Source/Name eq '\(category)'")
let api = NSURLQueryItem(name:"ZUMO-API-VERSION", value:"2.0.0")
components.queryItems = [expand, filter, api]

Gives you this URL from components.URL:

https://swoup.azurewebsites.net/tables/coupon?expand=source&filter=Source/Name%20eq%20\'Recommended\'&ZUMO-API-VERSION=2.0.0

jrturton
  • 118,105
  • 32
  • 252
  • 268