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
java kotlin httpclient backend
add a comment |
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
java kotlin httpclient backend
add a comment |
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
java kotlin httpclient backend
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
java kotlin httpclient backend
asked Nov 15 at 8:07
Bob
111
111
add a comment |
add a comment |
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"
})
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
add a comment |
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"
})
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
add a comment |
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"
})
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
add a comment |
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"
})
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"
})
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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