0

In my POST request , my URL throws an "unexpectedly found nil while unwrapping an Optional value" error. This is what it looks like:

    let imageUrl = NSURL(string: "http://88.143.30.77/app_backend/public/api/v1/image?_r={rec_id}")!
    let request = NSMutableURLRequest(URL: imageUrl)
    request.HTTPMethod = "POST"

If I remove the "?_r={rec_id}" part in the URL, the error goes away, but I have no idea how to fix it, because that's the URL I need to send my image to. Any help is much appreciated!

Nch
  • 75
  • 3
  • 14
  • That URL is invalid because it cannot contain `{` and `}`. You will have to encode the query parameters first. – Sulthan Mar 22 '16 at 11:45

2 Answers2

1

I suspect the {rec_id} should be replaced with an actual value. You should have something like this:

let imageUrl = NSURL(string: "http://88.143.30.77/app_backend/public/api/v1/image?_r=123456")!
let request = NSMutableURLRequest(URL: imageUrl)
request.HTTPMethod = "POST"

Documentation often uses angled brackets or curly braces to indicate where a value should be inserted. The angled brackets or curly braces themselves, along with the placeholder value, are what is replaced.

paulvs
  • 11,963
  • 3
  • 41
  • 66
0

I found the fix here: NSURL found nil while unwraping an Optional value .

I needed to encode the whole string

Community
  • 1
  • 1
Nch
  • 75
  • 3
  • 14