Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 32 lines (27 sloc) 652 Bytes
#!/usr/bin/env amm
// HttpApi.sc
import scalaj.http._
@main
def addPost(title: String, body: String) = {
ujson.read(
Http("http://jsonplaceholder.typicode.com/posts")
.postForm(Seq("title" -> title,
"body" -> body,
"userId" -> "1"))
.asString
.body
).obj.get("id").map(_.num.toInt).getOrElse(0)
}
@main
def comments(postId: Int) = {
val json = ujson.read(
Http(s"http://jsonplaceholder.typicode.com/comments?postId=$postId")
.asString
.body
)
val names = for{
item <- json.arr
name <- item.obj.get("name")
} yield name.str
names.mkString(",")
}