因此,您可以找到here的Play文檔給出了一個非常簡單的GET調用示例,沒有任何像auth頭或參數。斯卡拉測試Web服務客戶端的POST請求
有人能幫我弄清楚如何使用類似的策略來處理更復雜的事情,比如帶有JSON數據的POST請求,並且需要身份和auth頭文件?我現在無法正常工作。
我想知道如何使用ws來測試客戶端來執行外部http請求。
感謝
因此,您可以找到here的Play文檔給出了一個非常簡單的GET調用示例,沒有任何像auth頭或參數。斯卡拉測試Web服務客戶端的POST請求
有人能幫我弄清楚如何使用類似的策略來處理更復雜的事情,比如帶有JSON數據的POST請求,並且需要身份和auth頭文件?我現在無法正常工作。
我想知道如何使用ws來測試客戶端來執行外部http請求。
感謝
這裏是代碼我的項目之一片段通過其API twilio發送短信:
class SmsServiceImpl @Inject()(config: Configuration, ws: WSClient) extends SmsService {
val twilloAccountSid = config.getString("twillo.accountSid").get
val twilloAuthToken = config.getString("twillo.authToken").get
val smsApiUrl = config.getString("twillo.apiBaseUrl").get + "/" +twilloAccountSid+"/Messages.json"
override def send(phone: String, message: String): Future[Unit] = {
val data = Map(
"To" -> Seq(phone),
"From" -> Seq(config.getString("twillo.fromPhone").get),
"Body" -> Seq(message)
)
val response: Future[WSResponse] = ws.url(smsApiUrl).withAuth(twilloAccountSid, twilloAuthToken, WSAuthScheme.BASIC).post(data)
response.map { response =>
response.status match {
case 201 => Unit
case _ => throw new SmsSendingException(s"Failed to send sms. Got https status: ${response.status}")
}
}
}
它與身份驗證POST請求。
我也搜索了Play存儲庫尋找一些例子,但沒有。 – Jeep87c