使用curl方法调用API

lf5gs5x2  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(222)

这是我的卷发:

curl -X POST "http://bc.itg.ro:1239/api/ro/products" -H "accept: application/json" -H "X-Accept-Token: 7b030f02cce38a84d35122b5f353faq1" -H "Content-Type: application/json" -d "{ \"language_id\": 2, \"currency_id\": 4, \"filters\": { \"category_ids\": [ 56 ] }, \"current_page\": 1, \"limit\": 20}"

我想调用api以获取JSON

let xhr = new XMLHttpRequest();

          xhr.onreadystatechange = function() {
            //  Check if request is completed
            if (xhr.readyState == XMLHttpRequest.DONE) {
              //    Do what needs to be done here
              console.log(xhr.response);
              }
          }

          // Set the request URL and request method
          xhr.open("POST", "http://bc.itg.ro:1239/api/ro/products");

          // Set the `Content-Type` Request header
          xhr.setRequestHeader("accept", "application/json");
          xhr.setRequestHeader("X-Accept-Token", "7b030f02cce38a84d35122b5f353faq1");
          xhr.setRequestHeader("Content-Type", "application/json");

          // Send the requst with Data
          xhr.send('{\"language_id\": 2, \"currency_id\": 4, \"filters\": { \"category_ids\": [ 56 ] }, \"current_page\": 1, \"limit\": 20}');
      }
qzlgjiam

qzlgjiam1#

试试看:

fetch('http://bc.itg.ro:1239/api/ro/products', {
    method: 'POST',
    headers: {
        'accept': 'application/json',
        'X-Accept-Token': '7b030f02cce38a84d35122b5f353faq1',
        'Content-Type': 'application/json'
    },
    // body: '{ "language_id": 2, "currency_id": 4, "filters": { "category_ids": [ 56 ] }, "current_page": 1, "limit": 20}',
    body: JSON.stringify({
        'language_id': 2,
        'currency_id': 4,
        'filters': {
            'category_ids': [
                56
            ]
        },
        'current_page': 1,
        'limit': 20
    })
});

相关问题