title: How to add X-API-Key and X-Session to header using Retrofit library?
body: <p>I want to use <code>Retrofit</code> library instead of <code>HttpURLConnection</code> for my <code>multipart request</code> to send image to server. My <code>HttpURLConnection</code> request is not working for some reason. </p>
  
  <p>But I have problem in <code>Retrofit</code> and I don't know how to add <code>Session token</code> and <code>API key</code> to my request. </p>
  
  <p>This is solution I'm using for every request with <code>HttpURLConnection</code>(and its working for everything but multipart request):</p>
  
  <pre><code>val con = url.openConnection() as HttpURLConnection
  con.apply {
      requestMethod = method
      useCaches = false
      doOutput = true
      doInput = true
      addRequestProperty(HEADER_CONNECTION, "Keep-Alive")
      addRequestProperty(HEADER_CACHE_CONTROL, "no-cache")
      addRequestProperty(HEADER_CONTENT_TYPE, "multipart/form-data")
      addRequestProperty("X-API-Key", apiType.apiKey)
      addRequestProperty("X-Session", getServerSession())
  }
  </code></pre>
  
  <p>And this is my request using Retrofit library:</p>
  
  <pre><code>val f = File(app.cacheDir, filename)
  f.createNewFile()
  
  val fos = FileOutputStream(f)
  fos.write(data)
  fos.flush()
  fos.close()
  
  val reqFile = RequestBody.create(MediaType.parse("image/*"), f)
  val body = MultipartBody.Part.createFormData("upload", f.name, reqFile)
  
  val httpUrl = HttpUrl.Builder()
      .scheme("https")
      .host(mainUrl)
      .addPathSegment(apiSegment)
      .addQueryParameter("X-API-Key", apiType.apiKey)
      .addQueryParameter("X-Session", getServerSession())
  
  val service = Retrofit.Builder().baseUrl(httpUrl.build()).build().create(Service::class.java)
  val req = service.postImage(body)
  val response = req.execute()
  </code></pre>
  
  <p>I've used <code>addQueryParameter</code>, because I read that this is the way how to add those 2 parameters to header, but this will only affect my URL for API call and it will add API Key and Session to URL, which is not recognized by server at all.</p>
  
  <p>UPDATE: </p>
  
  <p>My Interface for Post Service: </p>
  
  <pre><code>internal interface MultipartService {
          @Multipart
          @POST("{url}")
          fun postImage(@Part image: MultipartBody.Part): Call&lt;ResponseBody&gt;
          fun setEndpoint(@Path("url") endpoint: String): Call&lt;ResponseBody&gt;
      }
  </code></pre>
  
  <p>UPDATE: FIXED</p>
  
  <pre><code>   internal interface MultipartServicePost {
          @POST("{url}")
          @Multipart
          fun postImage(@Part image: MultipartBody.Part, @Path(value = "url", encoded = true) endpoint: String): Call&lt;ResponseBody&gt;
      }
  </code></pre>