Search code examples
iosswiftnsmutableurlrequest

Adding token to httpheaderfield and escape character passing through


I have a token which contains \" in the string. When i print this string, the slash doesn't appear but quotation mark does. Which is correct.

token = "hhgghhgg\"hhgghhgg"
print -> hhgghhgg"hhgghhgg

request.setValue(token, forHTTPHeaderField: "API-Token")

When i add this token to the header field, the backslash for some reason passes through.

print all headers -> API-Token": "hhgghhgg\"hhgghhgg"

Im not sure if this is expected behaviour or an error.

Any help will be appreciated

many thanks


Solution

  • your code is correct and this is just some strange print behaviour. if you use a playground you can see that everything works as expected.

    enter image description here