Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get post parameters by request.param(name: "xxxx") #48

Open
ListenXunInNight opened this issue Jan 3, 2019 · 0 comments
Open

Comments

@ListenXunInNight
Copy link

ListenXunInNight commented Jan 3, 2019

Here is my code

static func signIn(request: HTTPRequest, resp: HTTPResponse) {

    var pwd = request.param(name: "pwd")
    var email = request.param(name: "email")
    
    defer {
        resp.completed()
    }

    guard pwd != nil && email != nil else {
        resp.appendBody(string: Response.JSON(.lackParameters))
        return
    }
}

Here is my request

POST /account/signIn HTTP/1.1
Host: 192.168.118.63:8181
Content-Type: application/json
Cookie: CSRF-TOKEN=BF6E8C61-8D4C-4501-BBD1-BFC223F997BF; PerfectSession=11279782-E82C-43BE-B986-71EEF1FB84E7
Accept: */*
User-Agent: GRedHeart5/1.0 (com.shabi.GRedHeart5; build:1; iOS 10.3.3) Alamofire/4.8.0
Accept-Language: zh-Hans-CN;q=1.0
Content-Length: 41
Accept-Encoding: gzip;q=1.0, compress;q=0.5
Connection: keep-alive

{"pwd":"666666","email":"nice@gmail.com"}

Here is post parameters in request.postParams data structure

▿ 1 element
    ▿ 0 : 2 elements
- .0 : "{\"pwd\":\"666666\",\"email\":\"nice@gmail.com\"}"
- .1 : ""

why I can't get post parameters by this method

public extension HTTPRequest {
    
    /// Returns the first GET or POST parameter with the given name.
    /// Returns the supplied default value if the parameter was not found.
    public func param(name: String, defaultValue: String? = nil) -> String? {
        for p in self.queryParams
            where p.0 == name {
                return p.1
        }
        for p in self.postParams
            where p.0 == name {
                return p.1
        }
        return defaultValue
    }
}

Maybe I prefer ask you why design like this?

@ListenXunInNight ListenXunInNight changed the title How to get post parameters by request.param(name: "xxxx") Can't get post parameters by request.param(name: "xxxx") Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant