Score:0

Creating taxonomy terms via REST API using POST: proper formatting of json?

kr flag

On Drupal 9.4 here and I want to create taxonomy terms via the api. Is there a good example of what I need to POST to an API end point?

Score:0
kr flag

Yes, make sure you have taxonomy REST services enabled at: /admin/config/services/rest

From there, you need to just POST to the following url: mydomain.com/taxonomy/term?_format=json

The format of the json you need the following only: (fyi, its in javascript for a nodejs file running in the cli.)

const postData = JSON.stringify({
  vid: [{ target_id: 'vocabulary_name', target_type: 'taxonomy_vocabulary' }], 
  name: [{ value: "some name here" }], 
});

Just pass that data into https request. Keep in mind to add in your user / pass authentication.

const options = {                                                                                                                                                                                  
  hostname: 'mydomain.com',                                                                                                                                                                    
  path: '/taxonomy/term?_format=json',                                                                                                                                                             
  method: 'POST',                                                                                                                                                                                  
  headers: {                                                                                                                                                                                         
    'Content-Type': 'application/json',                                                                                                                                                              
    'Authorization': 'Basic ' + Buffer.from('myuser:password').toString('base64'),                                                                                                              
    'Content-Length': Buffer.byteLength(postData),                                                                                                                                                 
  },                                                                                                                                                                                             };      
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.