Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created November 7, 2023 19:46
Show Gist options
  • Save everaldomatias/c3c28bdab60d1d17c6146445e2f3a6ba to your computer and use it in GitHub Desktop.
Save everaldomatias/c3c28bdab60d1d17c6146445e2f3a6ba to your computer and use it in GitHub Desktop.
const handleSubmitBackup = () => {
// Definindo os detalhes do usuário que você quer criar
let rand = Math.floor(Math.random() * 999)
let userData = {
username: 'jane-'+rand,
password: 'secret',
email: 'jane-'+rand+'@example.com',
roles: ['']
};
// Fazendo uma requisição POST para a REST API do WordPress
fetch('/wp-json/wp/v2/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from('user:pass').toString('base64')
},
body: JSON.stringify(userData) // Transformando o objeto dos dados do usuário em JSON
})
.then(response => {
// Verificando se a requisição foi bem-sucedida
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Processando a resposta para JSON
})
.then(data => {
console.log(data); // Aqui você terá os dados do usuário recém-criado
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
const handleSubmit2 = () => {
let rand = Math.floor(Math.random() * 999)
let userData = {
username: 'jane-'+rand,
password: 'secret',
repeat_password: 'secret',
email: 'jane-'+rand+'@example.com',
};
fetch('/wp-json/endpoint/users/v1/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return response.json()
})
.then(data => {
console.log(data)
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment