2

我有一个功能正常的 shell 脚本,看起来像这样(在终端中工作):

curl -X POST --compressed -H "Content-Type:application/json" \
  -H 'x-api-user: hdfjdfpjefpoj' \
  -H 'x-api-key: fpjefpojwpfjmwefpj' \
  https://example.com/api/v1/user/...

但是,当我尝试在 Applescript using 中使用它时do shell script,出现错误:

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
sh: line 1: -H: command not found
sh: line 2: -H: command not found
sh: line 3: https://example.com/api/v1/user/...: No such file or directory

这是Applescript:

do shell script "curl -X POST --compressed -H 'Content-Type:application/json'
  -H 'x-api-user: hdfjdfpjefpoj'
  -H 'x-api-key: fpjefpojwpfjmwefpj'
  https://example.com/api/v1/user/..."

出于安全原因,我发现了一些线索表明它do shell script的行为与终端不完全相同。但我还没有找到如何解决这个问题的线索。

没有标头信息的相同 Applescript 成功传递了 URL,但我需要在标头中发送我的凭据。

4

1 回答 1

1

我认为这是因为您在多行上有命令。而不是一个

线索是sh: line 1: -H: command not found

这表明 shell 正在尝试查找名为-H的命令,但没有找到它。

In most cases commands are always the first word in a command line string.

So this means the -H options are being interpreted as a command because they are the first word on a line. -H is clearly an option to curl and should follow it in the same line.

try it on a single line.

do shell script "curl  -X POST --compressed -H 'Content-Type:application/json'  -H 'x-api-user: hdfjdfpjefpoj'  -H 'x-api-key: fpjefpojwpfjmwefpj'  https://example.com/api/v1/user/..."
于 2013-06-26T16:07:18.013 回答