REST API Export Problem

rest-api
#1

I’m having trouble getting control of the REST api export command. The best I can do based on the example on https://docs.losant.com/rest-api/device/#export is to get data sent to my email using the following Curl example (the token and id strings are disguised). Unfortunately, I cannot get it to recognize the start and end times. It sends all of my data, no matter what I do.

I’ve also tried implementing this using Electric Imp agent code, with similar results. Any tips or examples would be much appreciated.

curl -H ‘Content-Type: application/json’
-H ‘Accept: application/json’
-H 'Authorization: Bearer eyJhbGciOiJIUzI1… YOUR_API_ACCESS_TOKEN …k-wgdGW1szQM’
-d ‘{“start”:-3600,“end”:0}’
-X POST https://api.losant.com/applications/5c204… APPLICATION_ID …68b814/devices/5c282a… DEVICE_ID …043873/export?email=iphone@systemsofmerritt.com

0 Likes

#2

Hello @Frank_Braswell!

I was able to replicate this problem and find a solution! The cURL documentation states that “options and their parameters must be specified on the same line in the file, separated by whitespace, colon, or the equals sign.” Thus, since we are specifying the email in the URL, all of the parameters must also be in the URL. Calling them separately with -d must be causing them to be ignored.

Here is the cURL syntax with the parameters in the URL:

curl -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H 'Authorization: Bearer YOUR_API_ACCESS_TOKEN' \
    -X POST \
    https://api.losant.com/applications/5c59b82b0b719400072cXXXX/devices/5c79539a30bca900XXXXXXXX/export?email=julia@losant.com&start=-360000&end=0

One more quick note: I set my start value to -360000 (6 minutes), as my device only updates once a minute.

Let me know if this works!
Julia

0 Likes

#3

Hi Julia,

I still can’t get it to recognize the start and end parameters. Below is the transaction on my Mac terminal.

Thanks so much for your prompt response! - Frank

Franks-iMac:~ frankbraswell$ curl -H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXxxxxxxxxxxI4icqoBgqDaWgo_k-wgdGW1szQM' \
-X POST https://api.losant.com/applications/5c2xxxxxxx8b814/devices/5c28xxxxxxxx80873/export?email=iphone@systemsofmerritt.com&start=-360000&end=0
[1] 10271
[2] 10272
Franks-iMac:~ frankbraswell$ {"success":true}
[1]-  Done                   
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer xxxxxxxxxxxxxxVhZjkxM_k-wgdGW1szQM' -X POST https://api.losant.com/applications/5cxxxxxxxx0b814/devices/5c2xxxxxxx3873/export?email=iphone@systemsofmerritt.com
[2]+  Done                    start=-360000
Franks-iMac:~ frankbraswell$ 

–First Line of data
1550860794241,2019-02-22T18:39:54.241Z,40000c2a6911cfba,

–Last line of data
1553534657177,2019-03-25T17:24:17.177Z,40000c2a691127cd,3.6780138,0.37502229,0.85473979,1.5205886,272,64511,143584,-55,97.919853,1553534657,0,

0 Likes

#4

Hi Frank!

I am trying to replicate this problem to better assist. :grinning:

Could you post a screenshot of the attachment that is being sent to your email? Here is mine:
01%20PM

Thanks!
Julia

0 Likes

#5

Hi Julia,

Can you get the information from the email link? I also uploaded the screenshot. Did it come through?

-Frank


Hello,

Your data export for device ElectricImpDeviceLive is now ready. To download the export, please click the following URL or copy and paste it into your browser:

https://losant-data-exports.s3.us-west-2.amazonaws.com/5c2044aceaf910000868b814-5c282a4952fd670008043873-1537982658529-1553534658529.csv?AWSAccessKeyId=AKIAI3D42VHS4PYVKJ3Q&Expires=1553621384&Signature=qZGXeoDPxD0UmYRO9c3JBL8Zy9o%3D

Thanks for using Losant!
The Losant Team


1 Like

#6

Hello @Frank_Braswell,

I have been playing with this trying to replicate, but have not done so successfully. However, I have some resources for debugging and testing. There are multiple ways to export device data, so I am curious as to which will work successfully for you. Here are a couple of things to try:

cURL
This we have attempted, but not been able to limit successfully. Here is a screenshot of my terminal:

Postman
Postman is a tool that can be used for POST requests, and will format the request for you, you just have to fill out the parameters. I would be curious to see what would come through to your email when using Postman. Here is an example of mine:

Losant API Device Export
The Losant API Node has a Device Export Resource that can be easily configured within a workflow. You can also test with this one to see if it recognizes the start and end values.

Let me know if any of these work!
Julia

0 Likes

#7

Hi Julia,

I have the answer to the problem. After looking closely at your url, I noticed that the punctuation characters were escaped with a backslash:

…/export\?email\=ecn@systemsofmerritt.com\&start\=-1012000\&end\=0

The url below is what was giving us trouble:

…/export?email=ecn@systemsofmerritt.com&start=-1012000&end=0

I’m not sure why it even recognized the email, but it only does if it is the first thing after the “?”. I’m also not sure why I was having trouble with the Electric Imp http.post() request, but it seems to be working now. Perhaps I was feeding it a positive 360000 instead of a negative 360000.

Turns out the Postman app must take care of the special characters for us, so it isn’t a problem. (Thanks for recommending Postman! I think it will come in handy for lots of things.)

I really appreciate your help and patience!

-Frank

1 Like