Home > Net >  Multiple blank problem header in Retrofit 2 in Kotlin
Multiple blank problem header in Retrofit 2 in Kotlin

Time:09-22

I have two headers and one of them has 2 spaces, so the retrofit gives an error, the code block is as follows.

@Headers(
    "Authorization: Basic some-token",
    "Content-Type: application/json"
)
@POST("client/getsession")
fun getSession(
    @Body body: String
): Single<Session>

Error Message: : HTTP 500 Internal Server Error That I get.

disposable.add(
        oAPIService.getSession()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object : DisposableSingleObserver<Session>()
            {
                override fun onSuccess(t: Session) {
                    session.value = t
                    sessionError.value = false
                    sessionLoading.value = false

                    println("ok")
                    observeSessionData()
                }

                override fun onError(e: Throwable) {
                    println("Not Worked   :  "  e.localizedMessage )
                    sessionLoading.value = false
                    sessionError.value = true


                }
            })
    )

CodePudding user response:

In the way you used the headers there you would also add {}, like in the sample bellow.

public interface UserService {  
@Headers({
    "Accept: application/vnd.yourapi.v1.full json",
    "User-Agent: Your-App-Name"
})
@GET("/tasks/{task_id}")
Call<Task> getTask(@Path("task_id") long taskId);
}

Also, check this documentation, it presents other ways of using headers and they may fit your code: https://futurestud.io/tutorials/retrofit-add-custom-request-header

CodePudding user response:

The HTTP 500 you get is an error from the server. There is nothing wrong with having spaces in your headers. You should check your server's implementation and the logs.

Also since it's a post, your server might expect a body, which you don't provide in your code. That might be the reason why it responds with a 500

oAPIService.getSession("a valid body")
  • Related