I am trying to perform an HTTP request in form-data format to an external server using the Micronaut HTTP Client. However, when I make the request, the server returns an error as if the body is completely empty.
To test if my request was being sent correctly, I made a Node application to capture the request data, then I tested a validated request through postman calling this server, and the Java/Kotlin application also to this server. The content looks exactly the same, as you can see in the following.
Comparing Content of Application Request with Postman Request
If I change the URL in postman and make the same request, it will return success, as you can see in the following print:
Print of Postman Request
Print of a Empty Body Request on Postman
Log of Request
17:07:52.205 [Test worker] WARN i.m.h.c.n.ssl.NettyClientSslBuilder - HTTP Client is configured to trust all certificates ('insecure-trust-all-certificates' is set to true). Trusting all certificates is not secure and should not be used in production.
17:07:53.692 [default-nioEventLoopGroup-1-2] DEBUG i.m.h.client.netty.DefaultHttpClient - Sending HTTP POST to https://ocd.com/api.php
17:07:53.695 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - content-type: multipart/form-data; boundary=36c78120781912df
17:07:53.695 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - transfer-encoding: chunked
17:07:53.695 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - host: ocd.com
17:07:53.902 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - HTTP Client Response Received (200 OK) for Request: POST https://ocd.com/api.php
17:07:53.902 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - date: Mon, 22 Jul 2024 20:07:53 GMT
17:07:53.902 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - server: Apache
17:07:53.902 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - vary: Accept-Encoding
17:07:53.903 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - x-frame-options: SAMEORIGIN
17:07:53.903 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - x-xss-protection: 1;mode=block
17:07:53.903 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - x-content-type-options: nosniff
17:07:53.903 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - content-type: application/json; charset=utf-8
17:07:53.903 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - x-http2-stream-id: 3
17:07:53.904 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - content-length: 1320
17:07:53.904 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - Response Body
17:07:53.905 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - ----
17:07:53.905 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - {"code":400,"status":"Empty input array.nTransfer-Encoding: chunkednHost: ocd..comnContent-Type: multipart/form-data; boundary=36c78120781912dfnnnnnUSER: apachenHOME: /usr/share/httpdnORACLE_HOME: /usr/lib/oracle/21/client64/libnSCRIPT_NAME: /api.phpnREQUEST_URI: /api.phpnQUERY_STRING: nREQUEST_METHOD: POSTnSERVER_PROTOCOL: HTTP/2.0nGATEWAY_INTERFACE: CGI/1.1nREMOTE_PORT: 65321nSCRIPT_FILENAME: /app/ocd/prod/www/api.phpnSERVER_ADMIN: nCONTEXT_DOCUMENT_ROOT: /app/ocd/prod/wwwnCONTEXT_PREFIX: nREQUEST_SCHEME: httpsnDOCUMENT_ROOT: /app/ocd/prod/wwwnREMOTE_ADDR: 135.20.60.219nSERVER_PORT: 443nSERVER_ADDR: 135.122.129.127nSERVER_NAME: nSERVER_SOFTWARE: ApachenSERVER_SIGNATURE: nPATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binnHTTP_TRANSFER_ENCODING: chunkednHTTP_HOST: ocd..comnCONTENT_TYPE: multipart/form-data; boundary=36c78120781912dfnproxy-nokeepalive: 1nH2_STREAM_TAG: 000nH2_STREAM_ID: nH2_PUSHED_ON: nH2_PUSHED: nH2_PUSH: onnH2PUSH: onnHTTP2: onnSSL_TLS_SNI: ocd..comnHTTPS: onnUNIQUE_ID: Zp68GQVLiVT8r4HwgmdXsgAA0hUnFCGI_ROLE: RESPONDERnPHP_SELF: /api.phpnREQUEST_TIME_FLOAT: 1721678873.2902nREQUEST_TIME: 1721678873nnnnnnnnn","time":"00.0016s","data":[]}
17:07:53.905 [default-nioEventLoopGroup-1-2] TRACE i.m.h.client.netty.DefaultHttpClient - ----
Kotlin Code:
fun requestTickets() {
val uri: URI = UriBuilder.of(configuration.url)
.build()
val bodyBuilder = MultipartBody.builder()
.addPart("auth_user", configuration.authUser)
.addPart("auth_key", configuration.authKey)
.addPart("object", configuration.objects)
.addPart("method", configuration.method)
.addPart("queue_arr[]", "BRAZIL_SD")
.addPart("event_id_last", "28218371")
val body = bodyBuilder.build()
val req = HttpRequest.POST(uri, body)
//.contentType(MediaType.MULTIPART_FORM_DATA_TYPE)
.contentType(MediaType.MULTIPART_FORM_DATA)
//.contentType(MediaType.FORM)
// .contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
val res = httpClient.toBlocking()
.exchange(req, String::class.java)
}
Note I’ve changed the contentType for all those options commented above.
Thank you
- Changing MediaType
Roberto Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.