Sending POST Request to Twitter API with Play Framework 2.0
playframework-2.0, scala
Solution
I have found this to work:
WS.url("http://api.twitter.com/1/users/lookup.json?user_id="+listOfFollowers)
.sign(OAuthCalculator(Twitter.KEY, tokens))
.post("ignored")
.map { response =>
val screenName: Seq[String] = response.json match {
case res: JsArray => res.value.map{ value => (value \ "name").toString }
case _ => Seq("")
}
}
I've also made notes to revisit my code with every major upgrade of Play! to check if the above gets fixed, because this is obviously not right.
Problem
How do I send a POST request to twitter API using Play Framework 2.0 (with Scala)? The API I'm trying to call work with both GET and POST, and I've successfully called it using GET with this code: ``` val followersURL = "http://api.twitter.com/1/users/lookup.json?user_id=" + listOfFollowers.mkString(",") WS.url(followersURL) .sign(OAuthCalculator(Twitter.KEY, tokens)) .get() .map{ response => val screenName: Seq[String] = response.json match { case res: JsArray => res.value.map{ value => (value \ "name").toString } case _ => Seq("") } } ``` Then I tried to call the API using POST like this: ``` WS.url("http://api.twitter.com/1/users/lookup.json") .sign(OAuthCalculator(Twitter.KEY, tokens)) .post(Map("user_id"->listOfFollowers)) .map { response => val screenName: Seq[String] = response.json match { case res: JsArray => res.value.map{ value => (value \ "name").toString } case _ => Seq("") } } ``` It didn't work and I get this exception: ``` [error] play - Waiting for a promise, but got an error: null java.lang.NullPointerException: null at java.io.Reader.<init>(Unknown Source) ~[na:1.7.0_01] at java.io.InputStreamReader.<init>(Unknown Source) ~[na:1.7.0_01] at oauth.signpost.OAuth.decodeForm(OAuth.java:157) ~[signpost-core.jar:na] at oauth.signpost.AbstractOAuthConsumer.collectBodyParameters(AbstractOAuthConsumer.java:236) ~[signpost-core.jar:na] at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:96) ~[signpost-core.jar:na] at play.api.libs.oauth.OAuthCalculator.sign(OAuth.scala:106) ~[play_2.9.1.jar:2.0.1] ``` Since it says that the exception occurs on the OAuthCalculator, I try to comment out the `.sign` call, and it didn't throw any exception, but of course I didn't get the right result. Am I doing something wrong? What am I doing wrong, and why? How could I fix the problem? Thanks before.