Put call that fails with 400:
restClient.put()
.uri("/tickets/$ticketId")
.contentType(MediaType.APPLICATION_JSON)
.body(CloseTicketRequest())
.retrieve()
.body(String::class.java)
I already had "equivalent" code in python using requests library, so was a bit surprised.
When inspecting the HTTP request I noticed that the RestClient do not add the content-length automatically, while the requests library did that.
When missing, I get 400 bad request from the server.
I propose either:
- always automatically add content-length
- instead of or in addition of having a .contentLength() method, why not just have an option to set it automatically, why should I convert my payload to json and getting its length "manually"?
Comment From: bclozel
This is called out in the upgrade guide here.
Comment From: gregjotau
Thanks, that worked, adding this for others:
// Most implementations of ClientHttpRequestFactory do not buffer request bodies before sending them to the server.
// This means the Content-Length is not set, which Gorgias does not support for put requets.
private final val bufferingFactory: BufferingClientHttpRequestFactory =
BufferingClientHttpRequestFactory(SimpleClientHttpRequestFactory())
private val restClient = RestClient.builder()
.defaultHeaders {
it.setBasicAuth(apiUsername, apiToken)
}.baseUrl("https://$subdomain.gorgias.com/api")
.requestFactory(bufferingFactory)
.build()