Search code examples
node.jsapache-kafkaavroconfluent-schema-registryavsc

Create a new avro schema using Kafka schema-registry API


I am trying to create a new schema using the kafka-schema-registery api. I followed the steps mentioned in this post. Register a new avro schema using Kafka schema-registry API

My Avro Schema:

{
  "doc": "Sample schema to help you get started.",
  "fields": [
    {
      "doc": "The id of the order.",
      "name": "orderId",
      "type": "int"
    }
  ],
  "name": "sampleRecord",
  "namespace": "com.mycorp.mynamespace",
  "type": "record"
}

Input parser:

 const inputSchemaJson = {
            "schema": JSON.stringify(inputSchema)
          }; 

Below is the implementation :

const https = require('https');

const options = {
                hostname: hostName,
                path: '/subjects/' + schemaName + '/versions',
                method: 'POST',
                json: true,
                body: inputSchemaJson,
                headers: {
                    'Content-Type': 'application/vnd.schemaregistry.v1+json',
                    'Authorization': 'Basic ' + authorizationHeader
                }
            };"

    const req = https.request(options, (res) => {
        let inputSchemaJson = '';

        console.log('Status Code:', res.statusCode);

        res.on('data', (chunk) => {
            inputSchemaJson += chunk;
        });

        res.on('end', () => {
            console.log('Body: ', JSON.parse(inputSchemaJson));
        });

    }).on("error", (err) => {
        console.log("Error: ", err.message);
    });

    req.write(inputSchemaJson);
    req.end();

But getting below error, Any suggestions.

 _http_outgoing.js:696
  throw new ERR_INVALID_ARG_TYPE('first argument',
        ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object

Solution

  • Error is self-descriptive

    argument must be of type string ... Received an instance of Object

    You're POST-ing a Javascript object. You'll need to completely stringify inputSchemaJson