NSErrorFailingURLStringKey (kCFErrorDomainCFNetwork error -1017) for specific requests

Jeehut picture Jeehut · Dec 17, 2014 · Viewed 19.5k times · Source

I wrote a helper class with which I make all my requests to a server (Rails 4.1). Most of the requests within this helper class work just fine, but one specific request is failing all the time. I'm using Alamofire for my requests.

Here's some of the code from my helper class:

let responseHandler = { (request: NSURLRequest, response: NSHTTPURLResponse?, object: AnyObject?, error: NSError?) -> Void in
    println("request: \(request) || response: \(response) || object: \(object) || error: \(error)")
}

// make custom request
let request = NSMutableURLRequest(URL: NSURL(string: fullPath)!)
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(["session_key": "ABC"], options: nil, error: nil)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = ["Accept": "application/json", "Content-Type": "application/json"]

Alamofire.request(request).response(responseHandler)

Here's the output from the println line in the above code:

request: <NSMutableURLRequest: 0x1702007e0> { URL: http:/wolverine.movie-assistor.staging.c66.me/user } || response: nil || object: Optional(<>) || error: Optional(Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be completed. (NSURLErrorDomain error -1017.)" UserInfo=0x1702ea780 {NSErrorFailingURLStringKey=http://wolverine.movie-assistor.staging.c66.me/user, NSErrorFailingURLKey=http://wolverine.movie-assistor.staging.c66.me/user, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSUnderlyingError=0x174240240 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1017.)"})

I tried resetting Simulator (since I read somewhere it might be a Simulator issue) and tried on my iPhone 6 – but the problem still stays the same. Trying the same URL with the CocoaRESTClient worked fine, so I doubt it's an issue on the server side (leaves to the expected response {"session_key" : "missing"}).

Anyone know what the issue is?

EDIT #1:

The server side which runs on Rails 4.1 looks as follows (application_controller.rb):

prepend_before_action :authenticate_session

def authenticate_session
  if (session_key = params[:session_key]).nil?
    forbidden(session_key: "missing")
  elsif (session = Session.find_by(session_key: session_key)).nil?
    forbidden(session_key: "unknown key")
  elsif session.open != true
    forbidden(session_key: "session closed")
  end
end

def forbidden(object = {})
  render json: make_hash(object), status: :forbidden
end

def make_hash(object)
  if object.is_a?(ActiveRecord::Base)
    return object.as_json
  end

  return object
end

EDIT #2:

In the mean time I tried different requests and it turns out that the same code on the server side works for all request types except for "GET" requests (I tried POST, PUT and DELETE specifically). So maybe the HTTP body must be different for GET requests?

Answer

Jeehut picture Jeehut · Dec 17, 2014

As I read here sending a HTTP body with a GET requests is against the HTTP/1.1 specification and thus is not supported on Rails. I didn't know that, so I will send my data via URL parameters. Maybe someone else also comes across this and this might help.