swisspol / GCDWebServer

The #1 HTTP server for iOS, macOS & tvOS (also includes web based uploader & WebDAV server)
Other
6.51k stars 1.25k forks source link

Post Handler Does not work #597

Open Semmarl opened 2 years ago

Semmarl commented 2 years ago

Get requests to server works and responsive but POST requests gives connection time out for iOS app.

[DEBUG] Did open connection on socket 11 [DEBUG] Did connect [DEBUG] Connection received 517 bytes on socket 11 [DEBUG] Did start background task [ERROR] No more data available on socket 11 [DEBUG] Connection aborted with status code 500 on socket 11 [DEBUG] Connection sent 116 bytes on socket 11 [DEBUG] Did close connection on socket 11 [VERBOSE] [192.168.1.110:8080] 192.168.1.106:53390 500 "(invalid request)" (517 | 116) [DEBUG] Did disconnect [DEBUG] Did end background task

Code:

`webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in

        return GCDWebServerDataResponse(html:"<html><body><p>Hello World</p></body></html>")

        })

    webServer.addDefaultHandler(forMethod: "POST", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in
        return GCDWebServerDataResponse(html:"<html><body><p>Hello World</p></body></html>")     
        })

    webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

    print("Visit \(webServer.serverURL) in your web browser")
    `

How can I fix this please?

lucaji commented 1 year ago

Hi, I havent been using GCDWebServer for a while, but I believe the "POST" handler method has to be added without the "default" handler specification. Please try following the README included in GCDWebServer "Advanced Example 2: Implementing Forms" as there is ax explicit example how to setup the POST method handler. It is written in Objective-C, but should be pretty easy to roughly translate it into Swift:

webserver.addHandler(forMethod: "POST", path: "/", request: GCDWebServerURLEncodedFormRequest.self, ....

please check if you are actually encoding a form or trying to accomplish something different. But again, I believe you are not supposed to handle any default POST requests without specifying a Path if I am not wrong. Try it out!

lucaji commented 1 year ago

But please note that what you are reporting as an Issue is not a bug of GCDWebServer, and as written in the README file, you are supposed to use StackOverflow to get help for your own projects.