Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert curl GET to javascript fetch

I am able to successfully retrieve an object from parse using command below:

curl -X GET \
-H "X-Parse-Application-Id:1231231231" \
-H "X-Parse-REST-API-Key: 131231231" \
-G \
--data-urlencode 'where={"uid":"12312312"}' \
https://api.parse.com/1/classes/birthday`

But I'm struggling converting it to javascript, using the code below I will get response with a status of 200. I assume error is converting data-urlencode to data:

var getObject = function {
    var url = "https://api.parse.com";
    url += '/1/classes/birthday';

    fetch(url, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'X-Parse-Application-Id': '12122',
            'X-Parse-REST-API-Key': '12121',
            'Content-Type': 'application/json',
        },
        data: '{"where":{"uid":"12312312"}}'

    })
    .then(function(request, results) {
        console.log(request)
        console.log(results)
    })
}

Thanks!

like image 553
user3430761 Avatar asked Jun 25 '15 01:06

user3430761


1 Answers

Since I often wanted to do this, I made https://kigiri.github.io/fetch/ , you can copy / paste a curl command it will give you the fetch code.

You can checkout the source if you want to use it differently.

edit: The feature was added to chrome devtools devtool-as-fetch-opts 👏👏 Thanks chrome dev team 🎉

like image 114
kigiri Avatar answered Sep 22 '22 21:09

kigiri