-1

I am making an iOS app that makes requests to an API. The request is in this format:

curl -X PUT -H "Content-Type: application/json" -d '{"username":"blabla@hotmail.com","password":"blabla"}' "https://server.mywebsite.com/login"

The API can only accept single-quoted strings in the body but I can't make a string with single quotes in Swift without it adding backslashes and making the string unreadable by the API.

"\'{\"email\": \"blabla@hotmail.com\", \"password\": \"blabla\"}\'"

Is there a way I can pass this string in Swift without the backslashes? Or is there a String or JSON encoding that is in that format?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
P. Stylianou
  • 55
  • 1
  • 7
  • `I can't make a string with single quotes in Swift without it adding backslashes` Of course [you can](https://www.evernote.com/l/AOyZGrjJjUZICq2zpvfXvhHdnNiurG7FJnA). Did you try? – Eric Aya Nov 25 '16 at 11:50
  • The single quotes in your curl are required just in the Unix shell, they are not actually transmitted to the server. The server just sees the JSON `{"username":"blabla@hotmail.com","password":"blabla"}`. So in your Swift you want just this: `"{\"email\": \"blabla@hotmail.com\", \"password\": \"blabla\"}"`. But I'm not sure whether this is your actual question. – hnh Nov 25 '16 at 11:51
  • @hnh You are right. But also like I mention, they could include the single quotes if they really need it. I think they don't really know themselves what they're asking. – Eric Aya Nov 25 '16 at 11:52
  • Yes I did. I get the error "Single quoted literal found, use """ " I can add backslashes to add the single-quotes but the backlashes remain in the string and are unreadable by the API – P. Stylianou Nov 25 '16 at 11:54
  • @P.Stylianou You can have single quotes in a normal Swift string, look at my screenshot in my first comment. It looks like you're confused about something. Please try to give us more details about your actual issue, not what you think is the issue. – Eric Aya Nov 25 '16 at 11:58
  • @EricAya Yes you are probably right about the single quotes, but even without them the backlashes remain in the request and the API doesn't recognise them – P. Stylianou Nov 25 '16 at 12:05

1 Answers1

1

The single quotes in your curl are required just in the Unix shell (to quote the double quotes on the command line), they are not actually transmitted to the server. The server just sees this JSON payload:

{"username":"blabla@hotmail.com","password":"blabla"}

So in your Swift API request you can remove the single quotes from your string:

let auth = "{\"email\": \"blabla@hotmail.com\", \"password\": \"blabla\"}"

Is there are way to avoid the escaping of the double-quotes here? No. In Swift you can't switch between ' and " like you can in say Python. Nor does it have """.

Since it is easy to make quoting errors when building the JSON on your own, you may want to use JSONSerialization instead, like so:

let jsonAuth   = [ "email":    "blabla@hotmail.com",
                   "password": "blabla" ]
let jsonData   = JSONSerialization.data(withJSONObject: jsonAuth)
let jsonString = String(data: data, encoding: .utf8)
hnh
  • 13,957
  • 6
  • 30
  • 40