How do you send raw byteArray as body of post request in khttp?











up vote
2
down vote

favorite












From the source of khttp it seems that you cant send raw byteArray as body of request because it always adds paddding to it. I've also tried using the Fuel library but it requires coroutines that conflict with my dependencies.



Does anyone know how to either 1) send raw bytes body in khttp or 2) another library that does










share|improve this question


























    up vote
    2
    down vote

    favorite












    From the source of khttp it seems that you cant send raw byteArray as body of request because it always adds paddding to it. I've also tried using the Fuel library but it requires coroutines that conflict with my dependencies.



    Does anyone know how to either 1) send raw bytes body in khttp or 2) another library that does










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      From the source of khttp it seems that you cant send raw byteArray as body of request because it always adds paddding to it. I've also tried using the Fuel library but it requires coroutines that conflict with my dependencies.



      Does anyone know how to either 1) send raw bytes body in khttp or 2) another library that does










      share|improve this question













      From the source of khttp it seems that you cant send raw byteArray as body of request because it always adds paddding to it. I've also tried using the Fuel library but it requires coroutines that conflict with my dependencies.



      Does anyone know how to either 1) send raw bytes body in khttp or 2) another library that does







      java kotlin httpclient backend






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 at 8:07









      Bob

      111




      111
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You're right. As per their code if the data you're sending is not file or stream it will be toString()'d which is not what you want. So, you may try providing a ByteArrayInputStream instead of ByteArray:



          val response = post(
          "https://httpbin.org/anything",
          data = ByteArrayInputStream(byteArrayOf(1, 2, 3)),
          headers = mapOf("Content-Type" to "application/octet-stream")
          )


          Thus you'll send bytes as is.



          BTW, khttp repo seems to be abandoned, so you'd better switch to another lib. Basically, any HTTP library can send raw bytes. As for the Fuel: it follows modular architecture and does not 100% require you to use coroutines:



          val (request, response, result) = "https://httpbin.org/anything".httpPost()
          .body(byteArrayOf(1, 2, 3))
          .header(mapOf("Content-Type" to "application/octet-stream"))
          .response()

          println(response)


          You'll see your byte array (in data):



          <-- 200 (https://httpbin.org/anything)
          Response : OK
          Length : 564
          Body : ({
          "args": {},
          "data": "u0001u0002u0003",
          "files": {},
          "form": {},
          "headers": {
          "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
          "Accept-Encoding": "compress;q=0.5, gzip;q=1.0",
          "Cache-Control": "no-cache",
          "Connection": "close",
          "Content-Length": "3",
          "Content-Type": "application/octet-stream",
          "Host": "httpbin.org",
          "Pragma": "no-cache",
          "User-Agent": "Java/1.8.0_192"
          },
          "json": null,
          "method": "POST",
          "origin": "1.2.3.4",
          "url": "https://httpbin.org/anything"
          })





          share|improve this answer



















          • 1




            Awesome, cheers
            – Bob
            Nov 16 at 6:41










          • Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
            – Bob
            Nov 16 at 8:35











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314886%2fhow-do-you-send-raw-bytearray-as-body-of-post-request-in-khttp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You're right. As per their code if the data you're sending is not file or stream it will be toString()'d which is not what you want. So, you may try providing a ByteArrayInputStream instead of ByteArray:



          val response = post(
          "https://httpbin.org/anything",
          data = ByteArrayInputStream(byteArrayOf(1, 2, 3)),
          headers = mapOf("Content-Type" to "application/octet-stream")
          )


          Thus you'll send bytes as is.



          BTW, khttp repo seems to be abandoned, so you'd better switch to another lib. Basically, any HTTP library can send raw bytes. As for the Fuel: it follows modular architecture and does not 100% require you to use coroutines:



          val (request, response, result) = "https://httpbin.org/anything".httpPost()
          .body(byteArrayOf(1, 2, 3))
          .header(mapOf("Content-Type" to "application/octet-stream"))
          .response()

          println(response)


          You'll see your byte array (in data):



          <-- 200 (https://httpbin.org/anything)
          Response : OK
          Length : 564
          Body : ({
          "args": {},
          "data": "u0001u0002u0003",
          "files": {},
          "form": {},
          "headers": {
          "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
          "Accept-Encoding": "compress;q=0.5, gzip;q=1.0",
          "Cache-Control": "no-cache",
          "Connection": "close",
          "Content-Length": "3",
          "Content-Type": "application/octet-stream",
          "Host": "httpbin.org",
          "Pragma": "no-cache",
          "User-Agent": "Java/1.8.0_192"
          },
          "json": null,
          "method": "POST",
          "origin": "1.2.3.4",
          "url": "https://httpbin.org/anything"
          })





          share|improve this answer



















          • 1




            Awesome, cheers
            – Bob
            Nov 16 at 6:41










          • Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
            – Bob
            Nov 16 at 8:35















          up vote
          0
          down vote













          You're right. As per their code if the data you're sending is not file or stream it will be toString()'d which is not what you want. So, you may try providing a ByteArrayInputStream instead of ByteArray:



          val response = post(
          "https://httpbin.org/anything",
          data = ByteArrayInputStream(byteArrayOf(1, 2, 3)),
          headers = mapOf("Content-Type" to "application/octet-stream")
          )


          Thus you'll send bytes as is.



          BTW, khttp repo seems to be abandoned, so you'd better switch to another lib. Basically, any HTTP library can send raw bytes. As for the Fuel: it follows modular architecture and does not 100% require you to use coroutines:



          val (request, response, result) = "https://httpbin.org/anything".httpPost()
          .body(byteArrayOf(1, 2, 3))
          .header(mapOf("Content-Type" to "application/octet-stream"))
          .response()

          println(response)


          You'll see your byte array (in data):



          <-- 200 (https://httpbin.org/anything)
          Response : OK
          Length : 564
          Body : ({
          "args": {},
          "data": "u0001u0002u0003",
          "files": {},
          "form": {},
          "headers": {
          "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
          "Accept-Encoding": "compress;q=0.5, gzip;q=1.0",
          "Cache-Control": "no-cache",
          "Connection": "close",
          "Content-Length": "3",
          "Content-Type": "application/octet-stream",
          "Host": "httpbin.org",
          "Pragma": "no-cache",
          "User-Agent": "Java/1.8.0_192"
          },
          "json": null,
          "method": "POST",
          "origin": "1.2.3.4",
          "url": "https://httpbin.org/anything"
          })





          share|improve this answer



















          • 1




            Awesome, cheers
            – Bob
            Nov 16 at 6:41










          • Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
            – Bob
            Nov 16 at 8:35













          up vote
          0
          down vote










          up vote
          0
          down vote









          You're right. As per their code if the data you're sending is not file or stream it will be toString()'d which is not what you want. So, you may try providing a ByteArrayInputStream instead of ByteArray:



          val response = post(
          "https://httpbin.org/anything",
          data = ByteArrayInputStream(byteArrayOf(1, 2, 3)),
          headers = mapOf("Content-Type" to "application/octet-stream")
          )


          Thus you'll send bytes as is.



          BTW, khttp repo seems to be abandoned, so you'd better switch to another lib. Basically, any HTTP library can send raw bytes. As for the Fuel: it follows modular architecture and does not 100% require you to use coroutines:



          val (request, response, result) = "https://httpbin.org/anything".httpPost()
          .body(byteArrayOf(1, 2, 3))
          .header(mapOf("Content-Type" to "application/octet-stream"))
          .response()

          println(response)


          You'll see your byte array (in data):



          <-- 200 (https://httpbin.org/anything)
          Response : OK
          Length : 564
          Body : ({
          "args": {},
          "data": "u0001u0002u0003",
          "files": {},
          "form": {},
          "headers": {
          "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
          "Accept-Encoding": "compress;q=0.5, gzip;q=1.0",
          "Cache-Control": "no-cache",
          "Connection": "close",
          "Content-Length": "3",
          "Content-Type": "application/octet-stream",
          "Host": "httpbin.org",
          "Pragma": "no-cache",
          "User-Agent": "Java/1.8.0_192"
          },
          "json": null,
          "method": "POST",
          "origin": "1.2.3.4",
          "url": "https://httpbin.org/anything"
          })





          share|improve this answer














          You're right. As per their code if the data you're sending is not file or stream it will be toString()'d which is not what you want. So, you may try providing a ByteArrayInputStream instead of ByteArray:



          val response = post(
          "https://httpbin.org/anything",
          data = ByteArrayInputStream(byteArrayOf(1, 2, 3)),
          headers = mapOf("Content-Type" to "application/octet-stream")
          )


          Thus you'll send bytes as is.



          BTW, khttp repo seems to be abandoned, so you'd better switch to another lib. Basically, any HTTP library can send raw bytes. As for the Fuel: it follows modular architecture and does not 100% require you to use coroutines:



          val (request, response, result) = "https://httpbin.org/anything".httpPost()
          .body(byteArrayOf(1, 2, 3))
          .header(mapOf("Content-Type" to "application/octet-stream"))
          .response()

          println(response)


          You'll see your byte array (in data):



          <-- 200 (https://httpbin.org/anything)
          Response : OK
          Length : 564
          Body : ({
          "args": {},
          "data": "u0001u0002u0003",
          "files": {},
          "form": {},
          "headers": {
          "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
          "Accept-Encoding": "compress;q=0.5, gzip;q=1.0",
          "Cache-Control": "no-cache",
          "Connection": "close",
          "Content-Length": "3",
          "Content-Type": "application/octet-stream",
          "Host": "httpbin.org",
          "Pragma": "no-cache",
          "User-Agent": "Java/1.8.0_192"
          },
          "json": null,
          "method": "POST",
          "origin": "1.2.3.4",
          "url": "https://httpbin.org/anything"
          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 at 8:40

























          answered Nov 15 at 8:32









          madhead

          13.7k1381116




          13.7k1381116








          • 1




            Awesome, cheers
            – Bob
            Nov 16 at 6:41










          • Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
            – Bob
            Nov 16 at 8:35














          • 1




            Awesome, cheers
            – Bob
            Nov 16 at 6:41










          • Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
            – Bob
            Nov 16 at 8:35








          1




          1




          Awesome, cheers
          – Bob
          Nov 16 at 6:41




          Awesome, cheers
          – Bob
          Nov 16 at 6:41












          Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
          – Bob
          Nov 16 at 8:35




          Found out that my conflicting dependencies was because I was using experimental coroutines. Upgraded Kotlin to 1.3.10 etc and it work fine. Thanks
          – Bob
          Nov 16 at 8:35


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314886%2fhow-do-you-send-raw-bytearray-as-body-of-post-request-in-khttp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents