S2Ler/ExpressSwift

parse data from a form

gurugeek opened this issue · 6 comments

I have reviewed your example:

express.use("/city/:name", .POST) { request, response in
    guard let cityName: String = request.getParameter("name") else { return true }
    
    print("Creating city with name: \(cityName)")
    citiesLock.lock(); defer { citiesLock.unlock() }
    cities.append(cityName)
    response.send("\(cities)")
    return false
}

but I am wondering how can I parse fields of an html form with POST ? request.getParameter is the :name in the url but I can't figure out how to parse post parameters.

S2Ler commented

POST parameters should be in body of the request. But there is no build in parser for that.

@diejmon thank you for your reply. as a work around I will try to send the form data as json! (I tried to parse request.body but all I get is some bytes)

S2Ler commented

You can try use json method on Request to parse body to Decodable type. Or just convert Data to string and to anything with it. :)

do you mean like this ?


struct Form: Codable {
  let name: String
  let email: String
  let message: String
}


express.use("/form", .POST) { request, response in

    let article = try! request.json(Form.self);
    

    print (article)
    
    print("article is : \(article)")

    response.send("post done")
    return false
}

I am now trying to get the form to send JSON to /form so it can be parsed (and then added to the database). If is not too much trouble can you show me how you would do it ? 👍

okay here is my full workaround:


first you need a form sending JSON data

<form id="myform" method="post" action="/form">
      Name: <input type="text" name="name" value="david"><br>
      email: <input type="text" name="email" value=""><br>
      message: <input type="text" name="message" value="this is a message "><br>
       <input type="submit" value="Submit">
      </form>



      <div id='response'></div>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.serializeJSON/2.9.0/jquery.serializejson.min.js"></script>
      <script>
     $("#myform").submit(function(){
       console.log($('#myform').serializeJSON());
       
       $.ajax({
         type: "POST",
         url: "/form",
         data: JSON.stringify($('#myform').serializeJSON()),
         success: function(data){alert("success posting!"+data);},
         error: function(data){alert("error posting!"+data);},
         contentType: "application/json"
       });
       return false;
     });
      
      </script>
</body>
</html>

then the struct and route


struct Form: Codable {
  let name: String
  let email: String
  let message: String
}


express.use("/form", .POST) { request, response in

    let article = try! request.json(Form.self);
    

    print (article.name)
    
    print("article is : \(article)")

    response.send("post done")
    return false
}

all working :) so now can add the single post elements to the database.

S2Ler commented

Nice! Closing the issue.