0

I need my Swift 2.1 app to listen for HTTP POSTs arriving on my public endpoint by another service. Not sure how to initialise GCDWebserver.

let webServer = GCDWebServer()

I tried:

webServer.addHandlerForMethod("POST", path: "https://myendpoint.com",
    requestClass: GCDWebServerRequest.self, asyncProcessBlock: { request in
    print("WebServer - POST detected")
})
webServer.startWithPort(80, bonjourName: "Web Server")

and

try! webServer.startWithOptions([GCDWebServerOption_BonjourName: "", GCDWebServerOption_BonjourType: "https://myendpoint.com", GCDWebServerOption_Port : 80, GCDWebServerOption_AutomaticallySuspendInBackground: false])

and

webServer.addHandlerForMethod("POST", path:"https://myendpoint.com", requestClass: GCDWebServerURLEncodedFormRequest.self, asyncProcessBlock: {request in
    print("WebServer: POST captured")
})
webServer.start()

but I am not getting anywhere.

Whatever I try, URL property points to my localhost and publicURL is always nil.

Any tips?

ppp
  • 713
  • 1
  • 8
  • 23
  • Make sure to read the `README` that comes with GCDWebServer as it explains how to use it with examples. – Pol Oct 29 '16 at 15:40

2 Answers2

0
  1. You can't start a webserver on port 80 on an iOS device. Using a port below 1024 needs root priviledges which your app don't have.

  2. a webserver path can't contain a domain name ... the webserver will always bind to the address of the device .. you just add the url of the endpoint. "path" would be http://<yourdeviceip>:<port>/path

Bastian
  • 10,403
  • 1
  • 31
  • 40
  • 3
    That's actually not true: you can open port 80 on your iPhone app, no root privileges needed. It's been a few years since they allowed that. – Pol Oct 29 '16 at 15:47
0

Your configuration is not correct, the idea behind it is that your application become a server itself that you can configure to accept incoming requests.

In your addHandlerForMethod you are setting the path to be "https://myendpoint.com": it doesn't make sense.

Your application will create a new server, so it will generate its own server URL (that should be the ip of your device followed by the custom port you choose).

I'm using it with Swift 2.3, so probably the syntax itself is a little different, but the idea is this one:

// Create the server
let webServer = GCDWebServer()

// Configure different paths
let path = "/action"
webServer.addHandlerForMethod("POST", path: action, requestClass: GCDWebServerRequest.self, asyncProcessBlock: { request in
    print("WebServer - POST detected")
    return GCDWebServerResponse(statusCode: 200)
})

// Start server on port 8080
webServer.startWithPort(8080, bonjourName: nil)

// Print server url
print("Server url: \(webServer.serverURL)")

With this configuration your server will be able to receive a POST request on the path

http://<your-device-ip>:8080/action
Marco Pace
  • 3,820
  • 19
  • 38
  • thx for clearing my confusion. the idea behind a "public" path is so that all devices running the app would get notified (i.e. get the POST), and not just a single device IP address. that's what I'm trying to get at. is there any way to do that? – ppp Oct 28 '16 at 11:34
  • I don't think so, at least not with that component: it creates a web server, so it means that every server on every device is unique. What you want to accomplish should be done sending a broadcast message, but you need to implement a different protocol. – Marco Pace Oct 28 '16 at 11:59
  • 1
    @Polis You can't do that only by running a web server on your iPhone app. First of all the server will only run while the app is in the foreground and 2nd of all you would need a server to keep track of the iPhones running your app and then sending a request to all of them. Use Apple push notifications or a pub-sub service like https://pusher.com/ or https://www.pubnub.com/. – Pol Oct 29 '16 at 15:49
  • @Pol is right - this is what [PubNub](http://pubnub.com) and [Pusher](http://www.pusher.com) do well. Going though the trouble of implementing a web server on your phone is a bit much and unnecessary. – Craig Conover Oct 30 '16 at 16:40
  • I now see that GCDWebServer is not the way to go for what I need. Any idea how I can accept JSON POST requests directly via my app? – ppp Oct 30 '16 at 18:53
  • If you want to accept JSON POST requests, then you need a webserver like GCDWebServer. But because of iOS limitations, these can't work in practice while your app is in the background. – Pol Nov 01 '16 at 19:40