Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizcarlospedrosogomes/2472c9ae5283daeb03c5265af65d5539 to your computer and use it in GitHub Desktop.
Save luizcarlospedrosogomes/2472c9ae5283daeb03c5265af65d5539 to your computer and use it in GitHub Desktop.
Create Request $Batch Odata V4
const oDataR = require('odata-batch-request');
const axios = require('axios')
const dotenv = require('dotenv');
dotenv.config();
function formatDateToYYYYMMDD(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // O mês começa em 0, então adicionamos 1 e formatamos com zero à esquerda, se necessário.
const day = String(date.getDate()).padStart(2, '0'); // Formata o dia com zero à esquerda, se necessário.
return `${year}-${month}-${day}`;
}
console.log(formatDateToYYYYMMDD(new Date()))
//const date = '20231007'//new Date().toISOString().substring(0, 10);
//const date = new Date()
const endpoint = encodeURIComponent(`center_cost(area='batc',center='batch',validate_date_end=2023-10-07)`)
const batch = new oDataR.ODataBatchRequest("http://localhost:4004/xsodata/solicitation.xsodata/", [
new oDataR.ODataBatchOperation("post",
"center_cost",
{
headers: {
"Content-Type": "application/json",
"Accept": "application/json;charset=UTF-8;IEEE754Compatible=true",
"IF-Match": "*"
},
body: (getReference) => JSON.stringify({area: 'refe', center: 'refe', validate_date_end: '2023-10-01'}),
}
),
new oDataR.ODataBatchOperation("put", endpoint, {
headers: {
"Content-Type": "application/json",
"Accept": "application/json;charset=UTF-8;IEEE754Compatible=true",
"IF-Match": "*"
},
body: JSON.stringify({
description: 'update $batch'
})
}),
new oDataR.ODataBatchOperation("put", endpoint, {
headers: {
"Content-Type": "application/json",
"Accept": "application/json;charset=UTF-8;IEEE754Compatible=true",
"IF-Match": "*"
},
body: JSON.stringify({
description: 'update $batch'
})
}),
])
//console.log(batch)
//return;
const config = {
method: 'post',
url: batch.url,
headers:{
...batch.headers,
'Accept-Charset': 'utf-8',
'Accept-Encoding': 'gzip, deflate',
// 'Accept-Language': 'en-US',
// 'Cache-Control': 'no-cache',
'Prefer': 'odata.continue-on-error',
'Authorization': `Bearer ${process.env.TOKEN}`,
},
//data: data
}
console.log(config.headers)
const data = batch.body
async function run(){
try {
const res = await axios.post(batch.url, data, { headers: config.headers })
const parsedRes = batch.parseResponse(res.data, batch.headers['Content-Type'])
console.log(parsedRes.operations)
console.log(parsedRes.operations[0].operation.getHttp())
} catch (error) {
console.log(error)
console.log(error.data)
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment