Your array should look something like this:
var payload =
{
title : "some title",
format : "link",
tags: [1,2],
categories: [3],
content: "http://someurl.com",
status: "publish"
};
The numbers 1 and 2 reference the tags with the ID 1 and 2 and likewise 3 calls the category with the ID 3.
I assume you don't want to reference only existing Tags and Categories, so you would have to first create them.
The following function uses an array that includes your tag names (tag 1 and tag 2) and posts it to the /wp-json/wp/v2/tags endpoint. If the tag already exists, it returns the ID of that tag. If the tag doesn't exist, then it creates a new tag and returns the ID of it.
postID is the ID of the post you want to add the tags to.
function getTagIDs(postID) {
let tagNames = [tag 1, tag 2];
let answers = tagNames.length;
let receivedAnswers = 0;
let tagIDList = [];
$.each(tagNames, function(i, tagName) {
let tagArguments = {
'name': tagName
};
let createPost = new XMLHttpRequest();
let postURL = phpVariables.siteBaseURL + '/wp-json/wp/v2/tags';
createPost.open('POST', postURL);
createPost.setRequestHeader('X-WP-Nonce', phpVariables.nonce);
createPost.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
createPost.onreadystatechange = function() {
if (this.readyState === 4) {
receivedAnswers++;
responseText = JSON.parse(this.responseText);
if (this.status === 400) {
tagID = responseText.data.term_id;
} else {
tagID = responseText.id;
}
tagIDList.push(tagID);
if (answers == receivedAnswers)
PostToWpApi(postID, tagIDList);
}
};
createPost.send(JSON.stringify(tagArguments));
});
};
Only after the function has saved all the tag IDs into the array tagIDList, will it execute another function called PostToWpApi.
function PostToWpApi(postID, tagIDs) {
let postMetaData = {
'tags': tagIDs,
};
let createPost = new XMLHttpRequest();
let postURL = phpVariables.siteBaseURL + '/wp-json/wp/v2/posts/' + postID;
createPost.open('POST', postURL);
createPost.setRequestHeader('X-WP-Nonce', phpVariables.nonce);
createPost.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
createPost.send(JSON.stringify(postMetaData));
};
I assume you have already done so, but for anyone else reading this, let me also explain where the phpVariables.siteBaseURL and phpVariables.nonce come from.
Those variables were first generated in PHP and made accessible to JS via wp_localize_script.
In whichever PHP file you enqueued your JS, you should also add the following:
public function enqueue(){
wp_enqueue_script('scripthandle', 'urltoscript.com/script.js');
wp_localize_script( 'scripthandle', 'phpVariables', array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'siteBaseURL' => get_site_url(),
));
}