V
V
Vapoltavecs2022-01-03 14:10:01
JavaScript
Vapoltavecs, 2022-01-03 14:10:01

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

3 answer(s)
D
Dmitry Belyaev, 2022-01-03
@Vapoltavecs

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',
  },
});

R
rPman, 2022-01-03
@rPman

--get --data-urlencode page=1

N
Nadim Zakirov, 2022-01-03
@zkrvndm

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 question

Ask a Question

731 491 924 answers to any question