0
votes

I want send a post request with a payload that include an array of Uint8Arrays to an API I'm building using kotlin and springboot.

The payload being sent up from the typescript client looks like this:

interface SavePatternContract {
  name: string
  frames: Array<Uint8Array>
  rows: number
  columns: number
  chunkSize: number
}

And I'm submitting it like so:

    const payload: SavePatternContract = {
      name: state.patternName,
      rows: state.frames[0].height,
      columns: state.frames[0].width,
      chunkSize: state.frames[0].chunkSize,
      frames: state.frames.map((frame) => frame.asByteArray()),
    }
    console.log(payload)

    const responseResult = await fetch(`${config.api.url}/animator/pattern`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(payload),
    })
    const response = await responseResult.json()

This works fine.

The issue I'm running into is figuring how to accept this request in springboot using kotlin.

On the server side I made a new PostMapping:

@RestController
@RequestMapping("/animator")
class PageController {

    @PostMapping("/pattern")
    fun savePattern(@RequestBody pattern: SavePatternContract) {
        print(pattern)
    }

And I thought I'd write the following data class to match the client's contract:

data class SavePatternContract(
    val name: String,
    val rows: Int,
    val columns: Int,
    val chunkSize: Int,
    val frames: ???
)

But I'm not sure how to handle the array of uint8arrays. I thought I could do Array<Uint8Array>, but it seems that Uint8Array isn't an available type (I'm new to kotlin and spring btw).

I thought it was because the type arrays have kotlin wrappers in the stdlib but I can't seem to access them.

I thought maybe org.khronos.webgl wasn't part of the standard library, but when I search maven central I can't find it.

From here I'm not sure if I'm just not including the right dependency in my pom to get the wrapper, or if this is event the right approach. I did try using Array<Array<Byte>> but that didn't work either.

I think this is me still being pretty green when it comes to kotlin. Any point in the right direction would be appreciated.

Have you tried UIntArray type?Михаил Нафталь
I have not! I can see that I can use the class so that's a major step forward. I'll try it out and if it works I'll write an update. Thanks for the point!!Chris Schmitz