Cloud API response to list devices using POST

I’m trying to get a list of devices using a post request. I’ve searched and searched for an answer to this question, I cannot find it. I want to use POST in order to limit visibility and exposure to URL-based tokens with GET.

Why does this GET work?

curl “https://api.particle.io/v1/devices?access_token=abc123456thisisnotmytoken
{ gives a full list of devices for this token….}

And does this POST fails?

curl -X POST -H “Authorization: Bearer abc123456thisisnotmytoken”
https://api.particle.io/v1/devices

returns:
{“ok”:false,“errors”:[“data.deviceID is empty”]}

Hi @craig

I think the answer is because fundementally GET means “read” data and POST means “write” data, which is a convention that not every web-based API adheres to, but Particle does.

Have you tried using the Authorization: Bearer header with the GET method? That should work.

2 Likes

Thanks for the quick reply, bko, it makes perfect sense.

I just tested it, and yes, that does work. Thanks for your help!

BTW, for anyone looking for Server side Java code to do this, the critical bits are:

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        ....... set timeouts or whatever
        conn.addRequestProperty("Authorization", "Bearer " + token);
        .......
        conn.connect();
1 Like

Thanks for the help!