我有一個RESTFul API服務,我想在POST請求中獲取參數(和頭文件)。有沒有關於它的信息在http://www.playframework.com/documentation/2.1.x/ScalaRouting獲取POST參數和頭文件
通過說,我有RESTFul API服務,我的意思是沒有查看頁面中。
我如何做到這一點?
我有一個RESTFul API服務,我想在POST請求中獲取參數(和頭文件)。有沒有關於它的信息在http://www.playframework.com/documentation/2.1.x/ScalaRouting獲取POST參數和頭文件
通過說,我有RESTFul API服務,我的意思是沒有查看頁面中。
我如何做到這一點?
這是因爲你想要的是Actions。
// param is provided by the routes.
def myApiCall(param:String) = Action { request =>
// do what you want with param
// there are some of the methods of request:
request.headers
request.queryString
Ok("") // probably there is an Ok with a better representation of empty. But it will give you a status 200 anyways...
}
更多Request
或者,如果你只是想PARAM:
def myApiCall(param:String) = Action {
//stuff with param
Ok("...")
}
對於這兩種情況的路線將如下所示:
POST /myapicall/:param WhateverClass.myApiCall(param)
這裏是一個小例子:
在路線:
GET /user/:name controllers.Application.getUserInfo(name)
在Application.scala
object Application extends Controller {
import play.api.libs.json._
import scala.reflect.runtime.universe._
/**
* Generates a Json String of the parameters.
* For example doing getJson(("status" -> "success")) returns you a Json String:
* """
* {
* "status" : "success"
* }
*/
def getJson[T: Writes](pairs: (String, T)*): String = {
val i = pairs.map { case (x, y) => (x -> (y: Json.JsValueWrapper)) }
Json.obj(i: _*).toString
}
def getUserInfo(name:String) = Action{ implicit request =>
val user = //get User object of name
Ok(getJson(("firstName" -> user.firstName),("lastName" -> user.lastName)))
}
//Directly calling Json.obj(("firstName" -> user.firstName),("lastName" -> user.lastName)).toString() will also do. getJson is used to make it more readable.
這不是一個發佈請求。 –
如何爲myApiClass路線是什麼樣子? –
如何找到標題及其值?我嘗試過'request.headers.keys.find(_ ==「myHeader」)',但這沒有任何意義。 –
老兄你的問題已被回答。我認爲現在是RTFM時間......或者如果您需要訓練,我可以將您指向我的鏈接。 – pedrofurla