Simple GET request

62 views
Skip to first unread message

Pete Cruz

unread,
Oct 21, 2014, 6:28:38 PM10/21/14
to raptur...@googlegroups.com
I'm having trouble invoking a simple GET request and I'm not sure what I'm doing wrong.

Here is what I'm importing.

import rapture.core._
import rapture.io._
import rapture.net._
import rapture.fs._
import rapture.codec._
import encodings.`UTF-8`

Here is a simple function that I'm trying to run.

def getUser(userName: String) = {
    val h = Http / "some_url" / "user" / userName /? Map('key -> "value")
    val response = h.get()
}

The error I'm receiving is...

[error] /Users/Petesta/Documents/scala-project/src/main/scala/scala-project/main.scala:35: an implicit TimeSystem is required; please import timeSystems.numeric or timeSystems.javaUtil
[error] Error occurred in an application involving default arguments.
[error]     val response = h.get()
[error]                         ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 5 s, completed Oct 16, 2014 3:36:10 PM

I have no idea what to do because I have tried to import both of those libraries, put a -Xlog-implicits flag in my sbt file but I receive the same error and no useful output is generated from the flag. What am I doing wrong?


Also does no one use the IRC channel?


Cheers,
Pete

Pete Cruz

unread,
Oct 21, 2014, 6:32:43 PM10/21/14
to raptur...@googlegroups.com
Also I wasn't able to run this from the introduction.md file in my repl after requiring `rapture.io._`


Is there an additional step not listed in the doc?

Jon Pretty

unread,
Oct 21, 2014, 7:07:46 PM10/21/14
to raptur...@googlegroups.com
Hi Pete,

For the second problem, the documentation is a bit vague on imports. I've got some improvements I should make to introduction.md. You will also need to import:

   import rapture.uri._
   import rapture.net._

Regarding the first problem, the `get` API requires a timeout to be specified, and allows different time systems to be used to specify the timeout. The easiest system to use is

   import timeSystems.numeric

but it looks like there's a limitation in the way default arguments can be provided for generic method parameters in Scala. I believe the solution should be to specify the value of the timeout explicitly, like this:

   import timeSystems.numeric
   val response = h.get(timeout = 10000L)

But I'm planning to overhaul the HTTP methods in the next month or so to try to find a way to avoid annoyances like this...

Let me know how it goes.

Cheers,
Jon

--
You received this message because you are subscribed to the Google Groups "Rapture users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapture-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jon Pretty | @propensive

Pete Cruz

unread,
Oct 23, 2014, 6:25:52 PM10/23/14
to raptur...@googlegroups.com
Hi Tom,

Thanks for the quick response it was very helpful! I thought timeout would have been handled by the implicit being brought into scope.

I have another question regarding JSON parsing.

I'm able to make the HTTP request and call slurp on the request to get new data out. But once I call Json.parse(src) I receive a JSON parser error.
I tried using Jackson and Spray and received a JSON parsing error for both of them. I'm aware there are other JSON libraries such as Argonaut (big fan) that you support.
Is there something that I'm doing wrong?

Thanks again!

//h = the get HTTP request
val src = h.slurp[Char]
val json = Json.parse(src)

Here are the stack traces for JSON parsing in Jackson and Spray.

1. Jackson
scala> val json = Json.parse(src)
org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.StringReader@28ba6171; line: 1, column: 2]
  at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
  at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
  at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
  at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198)
  at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485)
  at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)
  at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
  at org.codehaus.jackson.map.ObjectMapper.readTree(ObjectMapper.java:1542)
  at rapture.json.jsonBackends.jackson.JacksonParser$.parse(parse.scala:167)
  at rapture.json.jsonBackends.jackson.JacksonParser$.parse(parse.scala:158)
  at rapture.data.DataCompanion$$anonfun$parse$1.liftedTree1$1(data.scala:48)
  at rapture.data.DataCompanion$$anonfun$parse$1.apply(data.scala:48)
  at rapture.data.DataCompanion$$anonfun$parse$1.apply(data.scala:50)
  at rapture.core.modes$ThrowExceptions.wrap(modes.scala:105)
  at rapture.data.DataCompanion$class.parse(data.scala:47)
  at rapture.json.Json$.parse(json.scala:77)
  ... 40 elided

2. Spray
scala> val json = Json.parse(src)
rapture.core.ParseException
  at rapture.data.DataCompanion$$anonfun$parse$1.liftedTree1$1(data.scala:49)
  at rapture.data.DataCompanion$$anonfun$parse$1.apply(data.scala:48)
  at rapture.data.DataCompanion$$anonfun$parse$1.apply(data.scala:50)
  at rapture.core.modes$ThrowExceptions.wrap(modes.scala:105)
  at rapture.data.DataCompanion$class.parse(data.scala:47)
  at rapture.json.Json$.parse(json.scala:96)
  ... 40 elided

Jon Pretty

unread,
Oct 24, 2014, 3:39:18 AM10/24/14
to raptur...@googlegroups.com
Hi Pete,

Glad that worked!

It looks from the parse error that the source is wrong. Can you do a println on `src` just before attempting to parse it?

Thanks,
Jon
Reply all
Reply to author
Forward
0 new messages