Waiting for a HTTP request - scala

I am looking at this tutorial: http://lollyrock.com/articles/scala-http-requests/.
I'm trying to make a plain HTTP request like the one they have:
val address = url("http://somewebaddress.com/elements")
val x = Http(address OK as.String)
However, for that part of the code I am getting:
type mismatch;
[error] found : (com.ning.http.client.Request, dispatch.OkFunctionHandler[String])
[error] required: akka.actor.ActorSystem
[error] val x = Http(address OK as.String)
[error] ^
Which is quite strange since I'm passing only 1 argument, not 2.
Any idea why this is happening?

I was importing another HTTP library by accident and there was a name clash.

You have typo error in this string:
val response : Future[String] = Http(svc OK as.String)

Related

Cannot implement .extraInfoExtractor in Gatling Script

I am trying to log extra response information in gatling result through, http.extraInfoExtractor. Below is my code, I am failing to execute the same, also mentioned the error. Please help.
Code :
package cloudnative
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
class cloudnativems extends Simulation {
val nbUsers = Integer.getInteger("users", 1)
val myRamp = java.lang.Long.getLong("ramp", 0L)
val varPipelineId = sys.env.get("CI_PIPELINE_ID")
println(varPipelineId)
val httpProtocol = http
.baseUrl("https://lXXXXXXXXXX")
.inferHtmlResources()
.contentTypeHeader("application/json")
val scn = scenario("cloudnativems")
.exec(http("account_movement_post")
.post("/XXXXXXX/gatling-poc/demo/movement/account")
.extraInfoExtractor(extraInfo => List(extraInfo.response.statusCode.get))
.body(StringBody("""{
"movementId": "m0001",
"accountId": "a0001",
"amount": 2000,
"movementDate": "2019-02-26T09:34:50.301Z",
"counterparty": "c0001"
}""")))
.exec(http("account_movement_get")
.get("/XXXXXXX/gatling-poc/demo/movement/account/m0001")
)
setUp(scn.inject(atOnceUsers(1)).protocols(httpProtocol))
}
Error
C:\Sopra Project\Tools\gatling-charts-highcharts-bundle-3.0.3\bin>gatling.bat -s cloudnative.cloudnativems
GATLING_HOME is set to "C:\Sopra Project\Tools\gatling-charts-highcharts-bundle-3.0.3"
JAVA = ""C:\Program Files\Java\jdk1.8.0_201\bin\java.exe""
11:56:14.849 [ERROR] i.g.c.ZincCompiler$ - C:\Sopra Project\Tools\gatling-charts-highcharts-bundle-3.0.3\user-files\simulations\cloudnative\cloudnativems.scala:25:8: value extraInfoExtractor is not a member of io.gatling.http.request.builder.HttpRequestBuilder
possible cause: maybe a semicolon is missing before `value extraInfoExtractor'?
.extraInfoExtractor(extraInfo => List(extraInfo.response.statusCode.get))
^
11:56:15.190 [ERROR] i.g.c.ZincCompiler$ - one error found
11:56:15.193 [ERROR] i.g.c.ZincCompiler$ - Compilation crashed
sbt.internal.inc.CompileFailed: null
extraInfoExtractor has been dropped with version 3.0 according to https://gatling.io/docs/current/migration_guides/2.3-to-3.0/:
extraInfoExtractor was dropped as it wasn’t used in any Gatling component
I am not aware of a replacement construct in Gatling 3.0
Greetings,
Matthias

playframework-2.6: not enough arguments for method apply:

I am using Play-Framework-2.6 i am getting this error
/myproject/app/controllers/Application.scala:151: not enough
arguments for method apply: (data: akka.stream.scaladsl.Source[akka.util.ByteString, _], contentLength: Option[Long], contentType: Option[String])play.api.http.HttpEntity.Streamed in object Streamed.
[error] Unspecified value parameters contentLength, contentType.
[error] body = HttpEntity.Streamed(responseStream)
[error] ^
here is my code
def prometheusMetrics = Action {
val responseStream = Concurrent.unicast[Array[Byte]] { channel =>
val writer = new WriterAdapter(channel)
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples())
writer.close()
}
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Streamed(responseStream)
).as(TextFormat.CONTENT_TYPE_004)
}
i researched this but i did not find any suitable solution for it .please guide me
Update # 1
answer given by user #James Whiteley
after doing this
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Streamed(responseStream, None, None)
).as(TextFormat.CONTENT_TYPE_004)
i am getting
type mismatch;
[error] found : play.api.libs.iteratee.Enumerator[Array[Byte]]{implicit val pec: scala.concurrent.ExecutionContext}
[error] required: akka.stream.scaladsl.Source[akka.util.ByteString, _]
[error] body = HttpEntity.Streamed(responseStream, None, None)
HttpEntity.Streamed seems to take three parameters, not one. Try
body = HttpEntity.Streamed(responseStream, None, None)
if you don't want to specify contentLength and contentType - these are optional parameters but still need to be defined.

Gatling: Random Feeder POST request: not found: value email (Gatling 3.0.0)

**Randomized Load Testing with Gatling **
I want to test my built API for higher load, and therefore, I have to generate huge amount of random emails (I know there is currently no email-checking on the server, so I just create random strings).
The tool I use is gatling-charts-highcharts-bundle-3.0.0 and I checked out other questions and webpages on how to solve this, and I got the information to use feeders. I tried the following code, but I don't get why there is an value not error always upcoming.
Basically:
I'm trying to create a randomized string feeder and use it in gatling 3.0.0, but when i do it the following way, I always receive a " not found: value email" error:
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Random
class testsimu extends Simulation {
val httpProtocol = http
.baseUrl("http://testurl.com/api") // Here is the root for all relative URLs
.acceptHeader("application/json") // Here are the common headers
.acceptLanguageHeader("en;q=1.0,de-AT;q=0.9")
.acceptEncodingHeader("gzip;q=1.0,compress;q=0.5")
.userAgentHeader("TestApp/1.3.0(com.test.test;build:1;iOS12.1.0)Alamofire/4.7.0")
val headers_10 = Map("Content-Type" -> "application/json") // Note the headers specific to a given request
val feeder = Iterator.continually(Map("email" -> (Random.alphanumeric.take(20).mkString)))
val scn = scenario("Login") // A scenario is a chain of requests and pauses
// Note that Gatling has recorded real time pauses
.feed(feeder)
.exec(http("LoginRequest") // Here's an example of a POST request
.post("/login")
.headers(headers_10)
.body(StringBody(s"""{ "testmail": ${email}, "testmailagain": ${email}, }""")).asJson)
setUp(scn.inject(constantUsersPerSec(10) during(5 seconds) randomized).protocols(httpConf))
}
Here the error message:
GATLING_HOME is set to /Users/privat/Documents/test/Development/gatling-charts-highcharts-bundle-3.0.0
15:06:12.819 [ERROR] i.g.c.ZincCompiler$ - /Users/privat/Documents/test/Development/gatling-charts-highcharts-bundle- 3.0.0/user-files/simulations/test/test.scala:45:46: not found: value email
.body(StringBody(s"""{ "testmail": ${email}, "testmailagain": ${email}, }""")).asJson)
^
15:06:12.821 [ERROR] i.g.c.ZincCompiler$ - /Users/privat/Documents/test/Development/gatling-charts-highcharts-bundle-3.0.0/user-files/simulations/higgs/higgs-sim.scala:45:103: not found: value email
.body(StringBody(s"""{ "testmail": ${email}, "testmailagain": ${email}, }""")).asJson)
^
15:06:12.856 [ERROR] i.g.c.ZincCompiler$ - two errors found
15:06:12.858 [ERROR] i.g.c.ZincCompiler$ - Compilation crashed
sbt.internal.inc.CompileFailed: null
...
Why is there an "value not found" error coming?
I have found the solution:
Instead of:
StringBody(s"""{ "testmail": ${email}, "testmailagain": ${email}, }""")).asJson
Use:
StringBody("""{ "testmail": ${email}, "testmailagain": ${email}, }""")).asJson

Issue Creating Kinesis Stream with Scalazon

I'm following the Scalazon example at here to create a Kinesis stream. The following piece of code:
val streamListFuture = for {
s <- Kinesis.streams.list
} yield s
gives the following error:
[error] KinesisStatsWriter.scala:51: value map is not a member of object io.github.cloudify.scala.aws.kinesis.Requests.ListStreams
[error] s <- Kinesis.streams.list
If I don't use a for comprehension and call val createStream = Kinesis.streams.list, there's no error. Can't seem to figure out why.
Similarly, the following bit of code:
val createStream = for {
s <- Kinesis.streams.create(name)
} yield s
produces a similar error:
[error] KinesisStatsWriter.scala:64: value map is not a member of io.github.cloudify.scala.aws.kinesis.Requests.CreateStream
[error] s <- Kinesis.streams.create(name)
Appreciate the help!
Author here, the for-comprehension works only if you include the module that implicitly converts requests to Futures (it's called ImplicitExecution). Try adding the following import statement (looks at the sample code in the library README).
import io.github.cloudify.scala.aws.kinesis.Client.ImplicitExecution._

Get request with Rapture Http

I'm building an API with Rapture in Scala and having trouble resolving an issue with an implicit not being in scope. Here is the output from the error that I'm receiving.
[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
Here is the code that it is failing on.
def getUser(userName: String) = {
val h = Http / "some_url" / "user" / userName /? Map('key -> "value")
val response = h.get()
}
I'm not sure what to do because I've tried importing both libraries separately and the error is still the same.
I've also added the -Xlog-implicits flag to see if something else is causing the error but no additional information is outputted.
Is there a good resource anywhere with using the rapture-net library for HTTP requests? I couldn't find one except for Jon Pretty's slides at Scala By The Bay. I couldn't figure out a way to pass in a url with query strings into rapture-uri since it expects function invocation to look like this uri"url_dot_domain_with_query_strings".slurp[Char].
Any ideas?
The compilation error is not entirely correct in this case. You need 1 of the 2 imports AND you need to specify a timeout value.
def getUser(userName: String) = {
import timeSystems.numeric
val h = Http / "some_url" / "user" / userName /? Map('key -> "value")
val response = h.get(timeout = 5000L)
}
I don't really know of a good resource on it, but your basic single code line is correct. The biggest problem with the library is really documentation about the imports required. But this is what I found works for me:
def getGoogle() = {
import rapture.codec._
import rapture.io._
import rapture.uri._
import rapture.net._
import encodings.`UTF-8`
uri"http://google.com".slurp[Char]
}