LoginSignup
1
1

More than 5 years have passed since last update.

Simple Scala Script to Send HTTP Get Request

Posted at

Environment

OS: Ubuntu 16.04
Java: openjdk version "1.8.0_131"
Scala: 2.10
Build Tool: Apache Maven 3.5.0
IDE: IntelliJ

Add dependencies to pom.xml

To create a simple app which send http requests using REST API, scalaj-http package can be used. As a result, the following dependencies need to be added to pom.xml.

<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-connector-elasticsearch2_2.10</artifactId>
  <version>1.2.0</version>
</dependency>

Brief code review

An example function to send http get requests

The following code is an example to send http get requests to Ision REST API (or could be other storage system which support REST API). (Note that you need to define main() as well if you want to run this scala script.)

import scalaj.http.{Http, HttpOptions}

def send_request(storage_ip: String, api_path: String, user: String, password: String ) = {
    val api_url = storage_ip + api_path
    val http_response = Http(api_url).auth(user, password)
      .asString

    http_response.body
    }

Following sections are some explanation of the example code.

1. Import scalaj-http package

It is much similar to python.

import scalaj.http.{Http, HttpOptions}
2. Send a get http request

Using scalaj-http package is easy. Just passing "api url" to "Http" function, then response returns. According to Isilon OneFS API document, Isilon support HTTP Basic Authentication.

With HTTP Basic Authentication (RFC 2617), you can create a standard Authorization header with a valid username and password and send your request to the server.

And scalaj support HTTP Basic Authentication as well (source code). Thus, "auth()" header is added to the request.

val http_response = Http(api_url).auth(user, password)
3. HTTP response

Isilon API returns result in Json format. However, other information headers are also included in the response. Therefore, as you need only its body json data, set "body" explicitly.

    http_response.body

Memo: Default type returned by Scala function

What was tricky to me was default returned value by Scala function. At first, I defined a function like below but it didn't return anything as I expected. (What was new to me as well was, in Scala, you don't need to explicitly state "return" to say what value should be returned, however, you need to set a variable to return at the bottom of the function.)

  def test(x: Int, y: Int) {
    val z = x + y
    z
  }

According to this Stack Overflow post, by default, Scala function returns Unit type which is corresponding to Void in Java. That is why no value was returned by the above function. So you need to clarify what type to return or the function return Unit type. If you want to return "Int" type:

  def test(x: Int, y: Int): Int = {
    val z = x + y
    z
  }

Or if you want return any type, then, just use "=" like below.

  def test(x: Int, y: Int) = {
    val z = x + y
    z
  }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1