Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multipart form-data request with sttp - Scala HTTP client

Tags:

scala

How to do multipart form request using sttp library. Below is the sample curl request.

curl -X POST \
  http://localhost:2004/v2/api/artifacts \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F file=@/Users/username/filename

Below is the scala code.

val request = sttp
      .multipartBody(multipart("file", new FileInputStream(filePath)))
      .post(uri"$mistApiUrl/v2/api/artifacts")
val response = request.send()

The field file is not being sent properly.

Problem solved. Solution details here.

like image 900
Gowrav Avatar asked Dec 07 '25 03:12

Gowrav


1 Answers

fileName needs to be called upon using InputStream in multipart form-data.

val request = sttp
        .multipartBody(multipart("file", new FileInputStream(filePath)).fileName(fileName))
        .post(uri"$mistApiUrl/v2/api/artifacts")
like image 92
Gowrav Avatar answered Dec 08 '25 20:12

Gowrav