2013-10-08 40 views

回答

2

這是因爲你想要的是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) 
  • 公告:更名myApiClass到myApiCall,它是ori ginal意圖。
+0

如何爲myApiClass路線是什麼樣子? –

+0

如何找到標題及其值?我嘗試過'request.headers.keys.find(_ ==「myHeader」)',但這沒有任何意義。 –

+2

老兄你的問題已被回答。我認爲現在是RTFM時間......或者如果您需要訓練,我可以將您指向我的鏈接。 – pedrofurla

0

這裏是一個小例子:

在路線:

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. 
+0

這不是一個發佈請求。 –