ホーム

"Swift Perfect" Receive HTML post data

0
0


environment

  • swift
  • Perfect
  • Ubuntu

want to do

After sending posts in HTML, we will receive data of post at screen textile destination

POST from

POST to

実装

import PerfectHTTP

import PerfectHTTPServer

func handler(request: HTTPRequest, response: HTTPResponse) {
    
	response.setHeader(.contentType, value: "text/html")
    
	response.appendBody(string: """
    <form action="/PerfectTemplate/test" method="post">
      First name: <input type="text" name="fname"><br>
      Last name: <input type="text" name="lname"><br>
      <input type="submit" value="Submit">
    </form>
    """)
    
	response.completed()
}

func handler2(request: HTTPRequest, response: HTTPResponse) {

    response.setHeader(.contentType, value: "text/html")
    
    let uploads = request.postParams
    
    print(uploads)
    
    response.appendBody(string: """
        
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>HTML 5 complete</title>
    </head>
    <body>
    <p>\(uploads)</p>
    </body>
    </html>

    """)
    
    response.completed()
}

let confData = [
	"servers": [
		[
			"name":"localhost",
			"port":8182,
			"routes":[
				["method":"get", "uri":"/", "handler":handler],
                
                ["method":"post", "uri":"/test", "handler":handler2],
                
				["method":"get", "uri":"/**", "handler":PerfectHTTPServer.HTTPHandler.staticFiles,
				 "documentRoot":"./webroot",
				 "allowResponseFilters":true],
                
                ["method":"post", "uri":"/**", "handler":PerfectHTTPServer.HTTPHandler.staticFiles,
                 "documentRoot":"./webroot",
                 "allowResponseFilters":true]
			],
            
			"filters":[
				[
				"type":"response",
				"priority":"high",
				"name":PerfectHTTPServer.HTTPFilter.contentCompression,
				]
			]
		]
	]
]

do {
	try HTTPServer.launch(configurationData: confData)
} catch {
	fatalError("\(error)")
}

 

Pocket
LinkedIn にシェア

  • この記事を書いた人
  • 最新記事

藤沢瞭介(Ryosuke Hujisawa)

りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

-ホーム

Copyright© off.tokyo , 2021 All Rights Reserved Powered by AFFINGER5.