How to send a POST via axios to telegra.ph API?
I send a post to the telegraph API using axios. post () in nodejs. in response, I get that the token is invalid. although, if you send with the help of axios. get() , everything works out. I need post because get does not accept long texts since the data is passed in the url parameters
Perhaps I am passing the wrong object or something is not there where you need to zastringifail.
Here is the code:
const requestTelegraphPOST = async (url, data) => {
try {
return await axios.post(url, data)
} catch (error) {
console.error(error)
}
}
requestTelegraphPOST('https://api.telegra.ph/createPage', {
access_token: ACCESS_TOKEN,
title,
content
}).then(data => {
if (data.data.result) {
const telegraphPostUrl = data.data.result.url
sendPost(telegraphPostUrl)
} else {
console.log(chalk.yellow('Не удалось создать пост. Ответ от Telegraph API:'))
console.log(data.data)
}
}).catch(err => console.error(err))
Here is the API documentation https://telegra.ph/api
1
1 answers
Most likely, the empty request body goes away.
To send data via post
to axios
, you need a couple more points. You can try adding headers, in your case it will be:
const requestTelegraphPOST = async (url, data) => {
try {
return await axios.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
} catch (error) {
console.error(error)
}
}
Or there is an option to still collect data
through FormData
and already give it to post
:
const formData = new FormData();
formData.append('access_token', ACCESS_TOKEN);
...
I prefer the second option.
2
Author: Yaroslav Molchan, 2020-09-22 14:36:26