0

I am new to Scala Programming and I am actually Testing the Performance of API's using Gatling. The Thing is we don't want to store passwords in our code, So we want to call a REST API which returns the Username and Password.

The Catch here is the request type is a GET but it does have a JSON Body to send. The Response from the API Actually Depends the on the JSON Body we send in the Request

For Eg:

URL - https://www.somesecrets.com/ JSON Body -

{ 
  "env":"qa",
  "key":"micro"
}

Can someone help in writing a Scala code which allows to send Body in GET Request

This is some code I already tried


def get(url: String,
  connectTimeout: Int = 5000,
  readTimeout: Int = 5000,
  requestMethod: String = "GET") = {
  println("Getting Password from Secret Repo")
  import java.net. {
    URL,
    HttpURLConnection
  }
  val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection]
  connection.setConnectTimeout(connectTimeout)
  connection.setReadTimeout(readTimeout)
  connection.setRequestMethod(requestMethod)
  val inputStream = connection.getInputStream
  val content = scala.io.Source.fromInputStream(inputStream).mkString
  if (inputStream != null) inputStream.close
  content
}

But I don't understand how to expand this code in order to send a JSON Body

Coma
  • 89
  • 4
Yashwanth
  • 1
  • 1
  • 3

2 Answers2

0

You can't add body to GET request. GET requests include all required data in the URL, while POST requests supply additional data from the client (browser) to the server in the message body.

Here is an example how to execute POST request with scala:

import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import com.google.gson.Gson

object ExecutePostRequestApp extends App {

  val requestBody = s"""{ 
          "env":"qa",
          "key":"micro"
        }"""

  // create an HttpPost object
  val post = new HttpPost("https://www.somesecrets.com/")

  // set the Content-type
  post.setHeader("Content-type", "application/json")

  // add the JSON as a StringEntity
  post.setEntity(new StringEntity(requestBody))

  // send the post request
  val response = (new DefaultHttpClient).execute(post)
}
Gal Naor
  • 2,287
  • 11
  • 15
  • Unfortunately, I have to do a GET Request which has a JSON body,Thanks for letting me know how do the POST though. – Yashwanth Oct 13 '19 at 11:29
0

You cannot add a request body to a GET request. Well, technically you can but it won't mean anything. And a lot of HTTP clients won't allow you to. And Gatling probably doesn't allow you to. See more on that here.

The point of a GET request is that you are asking for the server to GET you some data. As soon as you send a request body, you are implying that some data needs creating or modifying on the server. This is why there are multiple request methods, and not everything is a GET.

If you want to send data in a GET request, you'll need to use query parameters, headers, cookies, session data... and if you need this to be hidden, encrypt it.

If you're stuck with having to send a body to a GET request, you need to reassess how your application is designed...


N.B. If I've misunderstood and you actually want to return a body from a GET request, let me know and I'll delete this answer as it no longer applies.

James Whiteley
  • 3,173
  • 1
  • 13
  • 35
  • No, You understood the question, But that is how the API was developed by the developers – Yashwanth Oct 16 '19 at 06:57
  • I guess this is the kind of stuff that you encounter when you go against convention. Maybe you could get your Gatling request to POST the data to a test-only endpoint, which then redirects to the GET route with the body from the POST request, but otherwise I worry a redesign is in order ([sending login information in a POST request would probably be your best bet](https://stackoverflow.com/a/5868860/8230810))... good luck to you on that one. – James Whiteley Oct 16 '19 at 09:28
  • Or you could store a dummy username/password in your app config and override it in your production environment? That keeps it out of your code/repo too. – James Whiteley Oct 16 '19 at 09:30