Shopify API works with cURL but not with XmlHTTPRequest?

BrickleRex
New Member
1 0 0

Hi. Im trying to use my private app to access the Shopify API. I've used it's keys to access the Orders API (uses POST) using cURL and it worked just fine. However when i try to reproduce the same result with the Javascript Fetch API or XMLHttpRequest, I dont get any response. The GET requests work just fine, but the POST ones don't, which is really weird. Here is the cURL code I used:

curl -X POST 'https://<username>:<password>@savethevalue.myshopify.com/admin/api/2020-04/orders.json' \
  -H 'Content-Type: application/json' \
  -d '{
    "order": {
      "line_items": [
        {
          "variant_id": 33025424588888,
          "quantity": 1
        }
      ],
      "financial_status": "pending"
    }
  }
  '

And here is the same code I wrote for the Fetch API.

fetch('https://<username>:<password>@savethevalue.myshopify.com/admin/api/2020-04/orders.json', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
    "order": {
      "line_items": [
        {
          "variant_id": 33025424588888,
          "quantity": 1
        }
      ],
      "financial_status": "pending"
    }
  }
  )
});

Could someone instruct me how to use Fetch to do so? Thanks a ton in advance.

0 Likes