Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Post Handler Does not work #597

Open
Semmarl opened this issue Nov 10, 2022 · 2 comments
Open

Post Handler Does not work #597

Semmarl opened this issue Nov 10, 2022 · 2 comments

Comments

@Semmarl
Copy link

Semmarl commented Nov 10, 2022

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
Copy link

lucaji commented Dec 7, 2022

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
Copy link

lucaji commented Dec 7, 2022

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants