Batch file Save CURL被输出到批处理文件中的一个变量中

Batch file Save CURL被输出到批处理文件中的一个变量中,batch-file,curl,Batch File,Curl,您好,这是我的第一篇帖子,所以如果我没有正确格式化某些内容,或者不在主题上,请让我知道 我需要使用cURL访问数据库,并提取云中存储的每个设备的数据点。到目前为止,我的问题是如何保存此行的访问令牌: `curl -X POST -d "<user><email>myemail@anexample.com</email><password>Passwordexample</password><application><a

您好,这是我的第一篇帖子,所以如果我没有正确格式化某些内容,或者不在主题上,请让我知道

我需要使用cURL访问数据库,并提取云中存储的每个设备的数据点。到目前为止,我的问题是如何保存此行的访问令牌:

`curl -X POST -d "<user><email>myemail@anexample.com</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`
这将传递令牌,但每次都需要手动输入。我曾尝试在不需要用户输入的情况下使用set,但一直无法使其正常工作。设置变量时,我是否缺少一些东西

任何帮助都将不胜感激


DM

假设CURL的XML响应如下所示:

<?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> 

0317080d361a430bb81e3997114267bf
c696753bddb4459c9a8ceb54fa04d53b
86400
OEM::员工
您可以尝试:

@echo off

set URL="<user><email>myemail@anexample.com</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
@echo关闭
设置URL=”myemail@anexample.comPasswordexampleanidexample_idasecretexample_secret"
对于/f“skip=2 tokens=3 delims=^”%%a in('curl-X POST-d%URL%H“内容类型:application/xml”https://user.aylanetworks.com/users/sign_in.xml""做"(
设置“$token=%%a”
后藤:下一个)
:下一个
回显令牌为:%$token%
暂停
curl-H“授权:授权令牌%$token%”https://ads-dev.aylanetworks.com/apiv1/dsns/AC000W000007000/properties.xml>TestFile.xml

在发送第二个请求之前,我暂停了
,您可以检查
%$token%

中是否有正确的值,其中哪个是令牌?命令输出如下:0317080d361a430bb81e3997114267bf c696753bddb4459c9a8ceb54fa04d53b 86400 OEM::工作人员感谢您的回答对我有用!我以前也用过类似的东西,我想知道你为什么写%%a而不是%a?你只有在
CMD
中直接运行
bat
文件中的命令时才使用%a。你必须将%%a增加一倍。哦,我忘了这个规则!你是对的,这会带来巨大的不同。再次感谢你,我真的很感激你的回答。
@echo off

set URL="<user><email>myemail@anexample.com</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