POST request sometimes hits the wrong endpoint in Akka-http program - scala

I have an Akka-http 2 server to handle HTTP requests.
On either endpoint, I'm converting to case class A or B:
case class caseClassA(data: String, eventID: String)
case class caseClassB(data: String, otherData: JsObject)
I have a routes object like this:
val routes = {
logRequestResult("akka-http-microservice") {
  pathPrefix("some_endpoint")
       (post & entity(as[caseClassA])) {
        Request =>
          val future = someFunction(Request)
            complete {
             future.map(result =>
             result
              )
            }
        } ~
 pathPrefix("other_endpoint") {
      (post & entity(as[caseClassB])) {
Request =>
          val future = otherFunction(Request)
          complete {
            future.map(result =>
             result
            )
          }
        }
      }
}
The problem: Sometimes, when calling from an iOS app using Alamofire, I'm getting an error response for bad data to some_endpoint when making a request for other_endpoint with a caseClassB-convertible JSON object:
[DEBUG] [01/22/2016 16:30:16.350] [ReactiveKafka-akka.actor.default-dispatcher-23] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for
  Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1))
  Response: Complete(HttpResponse(200 OK,List(),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1)))
[DEBUG] [01/22/2016 16:30:21.347] [ReactiveKafka-akka.actor.default-dispatcher-5] [akka://ReactiveKafka/user/$a/flow-17-2-prefixAndTail] Cancelling akka.stream.impl.MultiStreamOutputProcessor$SubstreamOutput#454147bb (after: 5000 ms)
[DEBUG] [01/22/2016 16:30:26.259] [ReactiveKafka-akka.actor.default-dispatcher-7] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for
  Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Default(application/json,319,akka.stream.scaladsl.Source#c3e53bd),HttpProtocol(HTTP/1.1))
  Response: Rejected(List(MalformedRequestContentRejection(Object is missing required member 'eventID',Some(java.util.NoSuchElementException: key not found: eventID)), TransformationRejection(<function1>), MalformedRequestContentRejection(Unexpected end-of-input at input index 0 (line 1, position 1), expected JSON Value:
^
,None), TransformationRejection(<function1>)))
As you can see by missing eventID, this is trying to convert to caseClassA. Both requests in the output above show I'm trying to hit other_endpoint. When I call this endpoint from Postman with the same data, it works 100% of the time.
What gives?

As seen in the comments above, the issue was missing braces ({}) around the code for "some_endpoint". Here's the corrected code block:
val routes = {
logRequestResult("akka-http-microservice") {
  pathPrefix("some_endpoint") {
      (post & entity(as[caseClassA])) {
         Request =>
         val future = someFunction(Request)
         complete {
          future.map(result =>
             result
           )
         }
}
     } ~
  pathPrefix("other_endpoint") {
       (post & entity(as[caseClassB])) {
Request =>
        val future = otherFunction(Request)
         complete {
           future.map(result =>
           result
          )
        }
       }
     }
}

Related

How to check for proper format in my API response

Currently running tests for my REST API which:
takes an endpoint from the user
using that endpoint, grabs info from a server
sends it to another server to be translated
then proceeds to jsonify the data.
I've written a series of automated tests running and I cannot get one to pass - the test that actually identifies the content of the response. I've tried including several variations of what the test is expecting but I feel it's the actual implementation that's the issue. Here's the expected API response from the client request:
{ "name": "random_character", "description": "Translated description of requested character is output here" }
Here is the testing class inside my test_main.py:
class Test_functions(unittest.TestCase):
# checking if response of 200 is returned
def test_healthcheck_PokeAPI(self):
manualtest = app.test_client(self)
response = manualtest.get("/pokemon/")
status_code = response.status_code
self.assertEqual(status_code, 200)
# the status code should be a redirect i.e. 308; so I made a separate test for this
def test_healthcheck_ShakesprAPI(self):
manualtest = app.test_client(self)
response = manualtest.get("/pokemon/charizard")
self.assertEqual(response.status_code, 308)
def test_response_content(self):
manualtest = app.test_client(self)
response = manualtest.get("/pokemon/charizard")
self.assertEqual(response.content_type,
'application/json') <<<< this test is failing
def test_trans_shakespeare_response(self):
manualtest = app.test_client(self)
response = manualtest.get("/pokemon/charizard")
self.assertFalse(b"doth" in response.data)
Traceback:
AssertionError: 'text/html; charset=utf-8' != 'application/json' - text/html; charset=utf-8 + application/json
Any help would be greatly appreciated

Akka-Http client: How to get binary data from an http response?

I call an API to get a zip file response. The API responds correctly but I am unable to get the byte array from response because the future that should complete on getting the ByteString never completes:
val authorization = akka.http.javadsl.model.headers.Authorization.basic("xxxxx", "xxxxxx")
val query = Map("fed" -> "xxxx", "trd" -> "yyy", "id" -> "zzz")
val request = HttpRequest(HttpMethods.GET, Uri("https://xxxx.yyyy.com/ggg/ttt.php").withQuery(Query(params = query))).addHeader(authorization)
val responseFut = http.singleRequest(request)
responseFut1.map(response => {
println("*******************************")
println(response)
response.status match {
case akka.http.javadsl.model.StatusCodes.OK => {
println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" + response._3)
val entityFut = response.entity.toStrict(60.seconds)
val byteStringFut = entityFut.flatMap(entity => {
entity.dataBytes.runFold(ByteString.empty)(_ ++ _)
})
println("#############################")
try {
byteStringFut.map(x => {
//this never prints =======================================problem
println("----------------------------" + x.toArray[Byte])
})
}catch{
case e: Exception => println("Error: " + e)
}
}
case _ => {}
}
})
If I print out the response this is what it looks like:
*******************************
HttpResponse(200 OK,List(Date: Fri, 08 Sep 2017 20:58:43 GMT, Server: Apache/2.4.18 (Ubuntu), Content-Disposition: attachment; filename="xxxxx.zip", Pragma: public, Cache-Contr
ol: public, must-revalidate, Content-Transfer-Encoding: binary),HttpEntity.Chunked(application/x-zip),HttpProtocol(HTTP/1.1))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^HttpEntity.Chunked(application/x-zip)
#############################
So response is coming back fine, but I still cannot get the binary data for the zip file.
We use akka-http in other places to call APIs that return json response and this approach seems to work fine there.
Why doesnt it work here? What am I doing wrong?
Any advice is appreciated.
Thanks.
Update
Adding byteStringFut.failed.foreach(println(_)) shows this exception: akka.http.scaladsl.model.EntityStreamException: HTTP chunk size exceeds the configured limit of 1048576 bytes
It looks like something went wrong and an exception was thrown in the async computation. You can check the exception by inspecting Future in the following way:
byteStringFut.failed.foreach(println)

HTTP4S client. How to get the exact request and response body

I am writing a small http4s client
val client = SimpleHttp1Client()
val uri = Uri.fromString(requestUrl).valueOr(throw _)
val task = POST(uri, UrlForm("username" -> userName, "password" -> password)).map{request => println("request: " + request.body)}
try {
val response = client.expect[String](task).unsafePerformSync
println("token: " + response)
response
} catch {
case e: Exception => println(e.getMessage);"BadToken"
}
The output is like
[info] Running com.researchnow.nova.shield.NovaShieldSetup
[info] Emit(Vector(ByteVector(44 bytes, 0x757365726e616d653d616268737269766173746176612670617373776f72643d41726)))
[info] Failed: unexpected HTTP status: 400 Bad Request
[info] token: BadToken
How to convert the binary request body to String? I want to see the body and headers in clear text.
I had a conversation with the http4s team on gitter and found the response. since gitter talk is not returned by google I am putting the answer here
val loggedReq = req.copy(body = request.body.observe(scalaz.stream.io.stdOutBytes))
println(loggedReq)
this prints all the headers. If we do something with the loggedReq then we get the entire body which is posted
loggedReq.as[String].run

How to check for 200 OK response status using Scala and Play Framework

I have a following Actor class that is responsible for sending a JSON message to a URL using POST.
import play.api.libs.ws._
class Worker extends Actor {
val logger: Logger = Logger("superman")
val supermanURL = "http://localhost:9000/superman/send"
def receive = {
case message: JsValue => {
val transactionID = (message \ "transactionID").get
println("Received JSON Object =>" + message)
val responseFromSuperman = WS.url(supermanURL).withHeaders("Content-Type" -> "application/json").post(message)
responseFromSuperman.map(
result => {
//TODO: Make sure to only log if response status is 200 OK
logger.info("""Message="ACK received from Superman" for transactionID=""" + transactionID)}
).recover { case error: Throwable =>
logger.error("""Message="NACK received from Superman" for transactionID=""" + transactionID) + " errorMessage:" + error.getLocalizedMessage()
}
}
}
}
So, if you look into my TODO above, I would like to add a check for a response type 200 OK. The current implementation is not doing that and it logs the message even if I manually send in a BadRequest. I tried checking for result.allHeaders which returns:
Map(Date -> Buffer(Wed, 27 Jan 2016 21:45:31 GMT), Content-Type -> Buffer(text/plain; charset=utf-8), Content-Length -> Buffer(7))
but no information about response status 200 OK
Simply:
import play.api.http.Status
if(result.status == Status.OK) {
// ...
}
Maybe I am missing here something but you have "status" on the response.
So you can do:
WS.url(url).withHeaders("Content-Type" -> "application/json").post(message).map{
case response if ( response.status == OK) => //DO SOMETHING?
}

Facebook test users can't do wall post

For our website, we do a lot of automated tests. So, recently I've just written a method using Facebook Graph API that create a wall post on feed. This method works when I test it live using real facebook accounts. However, when I use facebook test users (with permission set to "publish_stream"), then I get 403 forbidden.
Are "test users" not allowed to make wall post? or is there something that I am not doing right?
This is my test code written in groovy
void testPostMessageOnWall() {
def appAccessToken = facebookService.getAppAccessToken()
assertNotNull appAccessToken
def testUser1 = facebookService.createTestAccount("Ryoko UserOne", appAccessToken)
assertNotNull testUser1
def testUser2 = facebookService.createTestAccount("Ryoko UserTwo", appAccessToken)
assertNotNull testUser2
def response = facebookService.connectTwoTestAccount(testUser1.id, testUser1.access_token, testUser2.id, testUser2.access_token)
assertTrue response
println testUser1
println testUser2
def postResponse = facebookService.postMessageOnWall([accessToken:testUser1.access_token,
from:testUser1.id,
to:testUser2.id,
message:"Join ryoko.it. It's nice there!",
link:"http://ryoko.it",
name:"name of website",
caption:"ryoko.it",
description:"description",
picture:"http://ryoko.it/images/ryoko.png"
])
println postResponse
assertNotNull postResponse
facebookService.deleteTestAccount(testUser1.id, testUser1.access_token)
facebookService.deleteTestAccount(testUser2.id, testUser2.access_token)
}
This test makes two test users/accounts and make them friends of each other, then testUser1 post something in testUser2's wall. It fails in line: assertNotNull postResponse.
This is the header of the response:
Date: Thu, 01 Sep 2011 18:39:10 GMT
WWW-Authenticate: OAuth "Facebook Platform" "insufficient_scope" "(#200) The user hasn't authorized the application to perform this action"
P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
X-Cnection: close
X-FB-Rev: 433230
Content-Length: 146
Pragma: no-cache
X-FB-Server: 10.64.212.43
Content-Type: text/javascript; charset=UTF-8
Cache-Control: no-store
Expires: Sat, 01 Jan 2000 00:00:00 GMT
data:
{
"error": {
"type": "OAuthException",
"message": "(#200) The user hasn't authorized the application to perform this action"
}
}
The user is created as such:
def createTestAccount(fullname, appAccessToken) {
def accessToken = appAccessToken
def urlString = "${GRAPH_API_URL}/${APP_ID}/accounts/test-users?installed=true"
def encodedFullname = URLEncoder.encode(fullname, "UTF-8")
urlString += "&name=${encodedFullname}"
urlString += "&permission=create_note,email,offline_access,photo_upload,publish_stream,read_friendlists,share_item,status_update,video_upload"
urlString += "&method=post"
urlString += "&access_token=${accessToken}"
def url = new URL(urlString)
def connection = url.openConnection()
def userDetails
if (connection.responseCode == 200) {
userDetails = JSON.parse(connection.content.text)
}
else {
println "[FACEBOOK]\tResponse code ${connection.responseCode}: ${connection.responseMessage} [${urlString}]"
}
userDetails
}
and the post message goes like this:
def postMessageOnWall(params) {
assert params.accessToken
assert params.from
assert params.to
def content = "access_token=${postEncode(params.accessToken)}"
if (params.message) content += "&message=${postEncode(params.message)}"
if (params.link) content += "&link=${postEncode(params.link)}"
if (params.name) content += "&name=${postEncode(params.name)}"
if (params.caption) content += "&caption=${postEncode(params.caption)}"
if (params.description) content += "&description=${postEncode(params.description)}"
if (params.picture) content += "&picture=${postEncode(params.picture)}"
if (params.from) content += "&from=${postEncode(params.from)}"
if (params.to) content += "&to=${postEncode(params.to)}"
def urlString = "${GRAPH_API_URL}/${params.to}/feed"
def url = new URL(urlString)
def connection = url.openConnection()
connection.doOutput = true
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
connection.setRequestProperty("Content-Length", "${content.size()}")
println content
def writer = new OutputStreamWriter(connection.outputStream)
writer.write(content)
writer.flush()
writer.close()
connection.connect()
def response
if (connection.responseCode == 200) {
response = JSON.parse(connection.content.text)
}
else {
println "[FACEBOOK]\tResponse code ${connection.responseCode}: ${connection.responseMessage} [${urlString}]"
}
println "response: ${response}"
response
}
Even though it works using real facebook accounts (by filling in the id and access token manually), this still bothers me. I'm genuinely curious about you think the problem might be.
In your createTestAccount(), the query parameter 'permission' should be 'permissions' (in plural form).