Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save CURL is output into a variable in batch file

Hi this is my first post so if I haven't formatted something correctly or am not on topic please let me know.

I need to use cURL to access a database and extract datapoints for each device stored in the cloud. My question thus far is how do I save the access token from this line:

`curl -X POST -d "<user><email>[email protected]</email><password>Passwordexample</password><application><app_id>anidexample_id</app_id><app_secret>asecretexample_secret</app_secret></application></user>" -H "Content-Type:application/xml" https://user.aylanetworks.com/users/sign_in.xml`

Into a variable.

So far I've used :

 @echo off
    set /p UserInput= Enter the Access Token:
    @echo on

    curl -H "Authorization: auth_token %UserInput%" https://ads-dev.aylanetworks.com/apiv1/dsns/AC000W000007000/properties.xml>TestFile.xml

This passes the token but it needs to be entered manually every time. I've tried to use set without needing user input and I haven't been able to get it working. Is there something that I'm missing with setting variables?

Any help would be greatly appreciated,

DM

like image 908
Daniella Avatar asked Mar 21 '26 04:03

Daniella


1 Answers

Assuming that the CURL's XML response look like this :

<?xml version="1.0" encoding="UTF-8"?> 
 <authorization> 
   <access-token>0317080d361a430bb81e3997114267bf</access-token>
   <refresh-token>c696753bddb4459c9a8ceb54fa04d53b</refresh-token> 
   <expires-in type="integer">86400</expires-in> 
   <role>OEM::Staff</role> 
   <role-tags type="array"/> 
    </authorization> 

You can try :

@echo off

set URL="<user><email>[email protected]</email><password>Passwordexample</password><application><app_id>anidexample_id</app_id><app_secret>asecretexample_secret</app_secret></application></user>"

for /f "skip=2 tokens=3 delims=^<^>" %%a in ('curl -X POST -d  %URL% -H "Content-Type:application/xml" https://user.aylanetworks.com/users/sign_in.xml') do (
  set "$token=%%a"
  goto:next)

:next
echo The token is : %$token%
pause
curl -H "Authorization: auth_token %$token%" https://ads-dev.aylanetworks.com/apiv1/dsns/AC000W000007000/properties.xml>TestFile.xml

I putted a PAUSE before sending the second request that you can check if there is a correct value in %$token%

like image 56
SachaDee Avatar answered Mar 22 '26 23:03

SachaDee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!