Zhaoyu ZHONG
Zhaoyu ZHONG

Reputation: 133

How to convert a curl command to fetch()?

Here is my command:

curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/server/label_set --data 'SUBID=576965' --data 'label=example'

I have tried

fetch("https://api.vultr.com/v1/server/label_set", {
    method: "POST",
    headers: {
        "API-Key":"EXAMPLE"
    },
    data: "SUBID=576965&label=example",
})

and many others, but none of them worked.

Upvotes: 6

Views: 15001

Answers (2)

user3064538
user3064538

Reputation:

You can use curlconverter.com/javascript/. It converts your command to

fetch('https://api.vultr.com/v1/server/label_set', {
    method: 'POST',
    headers: {
        'API-Key': 'EXAMPLE'
    },
    body: new URLSearchParams({
        'SUBID': '576965',
        'label': 'example'
    })
});

Upvotes: 0

Herald Smit
Herald Smit

Reputation: 2531

You could try this tool: https://kigiri.github.io/fetch/

Source on github: https://github.com/kigiri/fetch

Output:

fetch("https://api.vultr.com/v1/server/label_set", {
       body: "SUBID=576965&label=example",
       header: {
       "API-Key": "EXAMPLE",
       "Content-Type": "application/x-www-form-urlencoded"
   }
})

Upvotes: 11

Related Questions