node.jsapache-kafkaavroconfluent-schema-registryavsc

Register a new avro schema using Kafka schema-registry API


I am trying to create a new schema using the kafka-schema-registery api. below is the implementation :

let value = JSON.stringify(avroSchema);

let type= {"schema" : value}; 

 fetch(`${process.env.SCHEMA_REGISTRY_URL}/subjects/${topic}/versions`,
        { 
          body : type,  
          method : 'POST', 
          headers :{ 'Content-Type': 'application/vnd.schemaregistry.v1+json,
                                      application/vnd.schemaregistry+json, application/json',
                     'Accept' : 'application/vnd.schemaregistry.v1+json,
                                 application/vnd.schemaregistry+json, application/json'            
        }
        })
    .then(res=>res.json())
    .then((result)=>{
        console.log('result is ', result);   
         resolve(result);    
     })
    .catch((err)=>{
        console.log('err',err);
        reject(err);
    })

Here is how avroSchema looks :

const avroSchema = {
      "type": "record",
      "name": "test",
      "fields" : [
            {"name": "field", "type": "long"},
    ]
  };

When I am executing this code I am getting 500 - Internal server error.

Can anyone help me to understand where I am going wrong ?


Solution

  • For future users :

    Here is how I was able to solve this :

     const payload = {
                       "schema": JSON.stringify(avroSchema)
                     }; 
    

    Finally after setting the payload, make a POST request options as below :

    const options = {
      method: 'POST',
      url: `${process.env.SCHEMA_REGISTRY_URL}/subjects/${topicName}-value/versions`,
      headers: { 'Content-Type': 'application/vnd.schemaregistry.v1+json' },
      body: payload,
      json: true
    }
    

    then make the request :

    request(options, function (error, response, body) {
        if (error) {
            reject(error);
        }                   
        resolve(body)
    });