0

SOLUTION: The API endpoint to get this specific information is the only one different from all the other ones. Thus I was trying to contact an outdated one.

Everything described in the post works like a charm.


Using kotlin and spring, I want to send a POST request to an API.

This is a curl, generated via postman, that works correctly:

curl --location --request POST 'URL' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username":"username",
    "password":"password"
}'

However, with spring + rest template using the following code, it seems that the body is not parsed correctly, as the API I am contacting sends me back a 401, that occurs when password/username provided are incorrect/missing.

val body = JwtQuery(getUsername(), getPassword())
return restTemplate.postForObject(url, body, Jwt::class.java)!!

This is the JwtQuery class:

data class JwtQuery (
        val username: String,
        val password: String
)

Note that getUsername() and getPassword() return the expected value.

What am I doing wrong ?


Edit 1: setting headers

Using:

val body = JwtQuery(getUsername(), getPassword())
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
val bodyEntity = HttpEntity(body, headers)
return restTemplate.postForObject(url, bodyEntity, Jwt::class.java)!!

Still returns the same error (401)


edit 2: marshaled JwtQuery

val body = JwtQuery(mesProperties.getUsername(), mesProperties.getPassword())
val marshaled = ObjectMapper().writeValueAsString(body)
println(marshaled)

Output: {"username":"username","password":"password"}

Itération 122442
  • 2,644
  • 2
  • 27
  • 73

0 Answers0