Answer the question
In order to leave comments, you need to log in
How to convert curl with --data option to fetch with get method?
Hello, I need to convert
curl -X GET http://127.0.0.1:8000/api/feed/posts/ --data "page=1" -H "Authorization: Token a96a3545bb566272f0fdfb405a0cccb7173da660"
in fetch with get method. How can I pass the page parameter there? i tried this site but it returns me fetch with post method
Answer the question
In order to leave comments, you need to log in
If data is passed to url as GET parameters:
const url = new URL('http://127.0.0.1:8000/api/feed/posts/');
url.searchParams.append('page', 1);
fetch(url, {
headers: {
Authorization: 'Token a96a3545bb566272f0fdfb405a0cccb7173da660',
},
});
Try like this:
(async function() {
try {
var response = await (await fetch('http://127.0.0.1:8000/api/feed/posts/', {
method: 'POST',
headers: {
'Authorization': 'Token a96a3545bb566272f0fdfb405a0cccb7173da660',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: 'page=1'
})).text();
console.log('Запрос успешно отправлен, ответ сервера:');
console.log(response);
}
catch(err) {
console.log('Запрос завершился неудачно, детали ниже:');
console.error(err);
}
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question