2

I have an endpoint who consumes Json with 2 attributes, like

{id='12344', data=byte_array} 

so I've wrote a test

Feature: submitted request

Scenario: submitted request
* def convertToBytes =
"""
function(arg) {
    var StreamUtils = Java.type('my.utils.StreamUtils');
    // it reads stream and convert it to a byte array
    return StreamUtils.getBytes(arg);
}
"""

 Given url 'http://my-server/post'
 And def image = convertToBytes(read('classpath:images/image_1.jpg'));
 And request {id:1, data: "#(image)"}
 When method POST
 Then status 200

However is got an exception form karate without much details

ERROR com.intuit.karate - http request failed: [B cannot be cast to [Ljava.lang.Object;

Any hits how to submit byte arrays as a part of Json with karate?

Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
osigida
  • 23
  • 3

1 Answers1

0

I don't think you can do that. Either the whole request should be binary (byte-array) or you do a multi-part request, where binary is Base64 encoded. As far as I know you can't put binary inside JSON. There is something called Binary JSON though.

EDIT: after assuming that the byte[] has to be Base64 encoded:

Background:
    * url demoBaseUrl
    * def Base64 = Java.type('java.util.Base64')

Scenario: json with byte-array
    Given path 'echo', 'binary'
    And def encoded = Base64.encoder.encodeToString('hello'.bytes);
    And request { message: 'hello', data: '#(encoded)' }
    When method post
    Then status 200
    And def expected = Base64.encoder.encodeToString('world'.bytes);
    And match response == { message: 'world', data: '#(expected)' }

I just added this test to the Karate demos, and it is working fine. Here is the commit.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • nope, this is not a solution for me, as the request is json with some binary data in it. I had no issues with jersey, so services talk to each other fine. Now I need to write some integration-test, and it looks like karate is not flexible in this terms Thanks a lot for the answer! – osigida Sep 28 '18 at 14:35
  • he, here how I serialise data with jersey: @JsonProperty("data") byte [] getData(); the rest is very common and default. Your example is not valid, karate-grpc is not karate but something else. I can create my glue code and use it... without karate at all. – osigida Sep 28 '18 at 20:23
  • @osigida great ! – Peter Thomas Oct 02 '18 at 09:32