Skip to content

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

@ListenXunInNight

Description

@ListenXunInNight

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions