我使用Ubuntu并在其上安装了Curl.我想用Curl测试我的Spring REST应用程序.我在Java端编写了我的POST代码.但是,我想用Curl测试它.我正在尝试发布JSON数据.示例数据如下:
{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}
Run Code Online (Sandbox Code Playgroud)
我用这个命令:
curl -i \
-H "Accept: application/json" \
-H "X-HTTP-Method-Override: PUT" \
-X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
http://localhost:8080/xx/xxx/xxxx
Run Code Online (Sandbox Code Playgroud)
它返回此错误:
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
Run Code Online (Sandbox Code Playgroud)
错误描述如下:
服务器拒绝此请求,因为请求实体的格式不受所请求方法()的请求资源支持.
Tomcat日志:"POST/ui/webapp/conf/clear HTTP/1.1"415 1051
关于Curl命令的正确格式的任何想法?
编辑:
这是我的Java端PUT代码(我测试过GET和DELETE,它们有效)
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
configuration.setName("PUT worked");
//todo If error occurs …Run Code Online (Sandbox Code Playgroud) 我想运行一个外部进程并将其命令输出捕获到PowerShell中的变量.我目前正在使用这个:
$params = "/verify $pc /domain:hosp.uhhg.org"
start-process "netdom.exe" $params -WindowStyle Hidden -Wait
Run Code Online (Sandbox Code Playgroud)
我已经确认命令正在执行但我需要将输出捕获到变量中.这意味着我无法使用-RedirectOutput,因为这只会重定向到文件.
如何使用转义双引号替换批处理文件参数中的所有双引号?这是我当前的批处理文件,它扩展了字符串中的所有命令行参数:
@echo off
call bash --verbose -c "g++-linux-4.1 %*"
Run Code Online (Sandbox Code Playgroud)
然后它使用该字符串调用Cygwin的bash,执行Linux交叉编译器.不幸的是,我将这些参数传递给我的批处理文件:
"launch-linux-g++.bat" -ftemplate-depth-128 -O3 -finline-functions
-Wno-inline -Wall -DNDEBUG -c
-o "C:\Users\Me\Documents\Testing\SparseLib\bin\Win32\LinuxRelease\hello.o"
"c:\Users\Me\Documents\Testing\SparseLib\SparseLib\hello.cpp"
Run Code Online (Sandbox Code Playgroud)
传入第一个路径的第一个引用是过早地结束传递给GCC的字符串,并将其余参数直接传递给bash(这非常失败.)
我想如果我可以将参数连接成一个单独的字符串然后转义它应该正常工作的引号,但我很难确定如何做到这一点.有人知道吗?
如果我调用
Rscript -e "print('hello')"
Run Code Online (Sandbox Code Playgroud)
它正确打印出答案
[1] "hello"
Run Code Online (Sandbox Code Playgroud)
但是,如果我切换单引号和双引号,则不起作用,并且看起来双引号被删除了:
Rscript -e 'print("hello")'
Run Code Online (Sandbox Code Playgroud)
给出:
Error in print(hello) : object 'hello' not found
Execution halted
Run Code Online (Sandbox Code Playgroud)
请注意,它不是 powershell 错误地进行转义。Echoing 只给出预期的结果:
PS> echo 'print("hello")'
print("hello")
PS> echo "print('hello')"
print('hello')
Run Code Online (Sandbox Code Playgroud)
在 macOs 或 Linux 上没有观察到相同的行为,这两种变体都被正确解析。
有趣的是,command.com 更疯狂:
C:>Rscript -e "print('hello')"
[1] "hello"
C:>Rscript -e 'print("hello")'
[1] "print(hello)"
Run Code Online (Sandbox Code Playgroud)
我的意思是……什么?!?这已经在这里提到了:
但没有关于它的解释。在我看来,这是 Windows 上 Rscript 的一个错误,但我想听听其他意见。
我在 Powershell 脚本中使用 WinSCP。它突然停止工作了。一段时间后,我发现问题是由更新版本的 PowerShell 出现的:
减少代码:
& winscp `
/log `
/command `
'echo Connecting...' `
"open sftp://kjhgk:jkgh@example.com/ -hostkey=`"`"ssh-ed25519 includes spaces`"`""
Run Code Online (Sandbox Code Playgroud)
使用 v7.2.7 时出现错误消息
主机“example.com”不存在。
使用 v7.3.0 时出现错误消息
命令“open”的参数太多。
正如您所看到的,在 v7.3.0 中,WinSCP 根据 PS 版本接收不同的输入。我发现差异与主机密钥中的空格有关。如果省略它们,v7.3.0 会输出相同的错误。
PowerShell 的哪些更改导致了此问题?如何修复它?(我如何调试此类问题?我尝试了一些转义,但无论版本如何,字符串看起来都一样,没有明显的重大更改可以负责)
我正在尝试使用包含空格的值在installshield安装程序中设置公共属性.在运行MSI安装程序时,我在PowerShell命令提示符下使用以下命令.由于该值包含空格,因此我使用双引号来传递值
msiexec -i "myinstaller.msi" MYDIRPATH="C:\new folder\data.txt"
Run Code Online (Sandbox Code Playgroud)
它打破了命令,因为参数值C:\new folder\data.txt在字符串中有一个空格,new folder并导致msiexec的错误提示下面,这表明传递给msiexec命令的参数有问题:
如果我在Windows默认命令shell提示符上运行相同的命令,那么它就像一个魅力.
我试过的选项:
powershell windows-installer installshield command-line-arguments msiexec
我怀疑没有好的解决方案,但也许我忽略了一些事情:
我所追求的是一种方法:
(a) 从 PowerShell 调用批处理文件,其方式可以在 PowerShell 自动变量中稳健地$LASTEXITCODE反映其(隐式或显式)退出代码。
值得注意的是,调用以 , 退出的批处理文件whoami -nosuch || exit /b应该会导致$LASTEXITCODE反映 的whoami退出代码,即1。当您从 PowerShell 调用批处理文件(按名称或路径)时,情况并非如此:退出代码为0(相反,在cmd.exe会话内部设置为%ERRORLEVEL% )1。
另请注意,调用应与 PowerShell 的输出流保持集成,因此我并不是在寻找基于System.Diagnostics.Process.
此外,我不知道或无法控制调用的批处理文件 - 我正在寻找通用的解决方案。
(b) 传递给批处理文件的双引号参数不会以任何方式被更改,并且cmd.exe的行为不会以任何方式被修改;尤其:
^字符不应该(见下文)。/V:ON选项。我知道如何解决(a)的唯一方法是通过调用批处理文件cmd /c call。
不幸的是,这违反了要求 (b),因为在参数中使用call看似总是双倍的^字符。(并且,相反,不使用 …
我的Powershell脚本需要使用一组非常复杂的参数调用EXE.我正在使用Powershell 3.0,并且必须坚持使用该版本.唉,即使是"魔术"逃脱的操作员(--%)也没有帮助我.例如,使用Call运算符,请考虑以下因素:
& other.exe --% action /mode fast /path:"location with spaces" /fancyparam { /dothis /dothat:"arg with spaces" } /verbose
Run Code Online (Sandbox Code Playgroud)
现在,如果它很简单,我的脚本可以很容易地正常工作.但事情并非那么简单."other.exe"的参数可能不同,具体取决于我脚本中较早的用户选择.所以相反,我需要提前建立这些参数,也许是这样的:
$commandArgs = 'action /mode ' + $userMode + ' /path:"location with spaces" /fancyparam { /dothis /dothat:"' + $userArgs + " } /verbose'
Run Code Online (Sandbox Code Playgroud)
因此我会这样调用:
& other.exe --% $commandArgs
Run Code Online (Sandbox Code Playgroud)
...好吧,期望这--%意味着它只是传递一个原始字符串$commandArgs.但是如果没有--%,powershell会自动引用$ commandArgs的内容,这些内容实际上会混淆内部引用(更不用说打破其他程序首先需要的'action'参数).换句话说,我已经尝试嵌入--%我的$ commandArgs字符串内部,但损坏已经在它被解析的时候完成了(我不认为它甚至可以这样工作).
请注意,这个例子只是我需要执行的实际命令的1/4 - 其中包括更多用户参数,引号和其他有趣的字符,这些字符会让我匆忙逃离地狱!我也一直在使用echoargs.exe工具,这就是我看到我遇到的麻烦.哦,我也需要我的例子中的所有空格(即需要在括号字符周围留出空格).
所以经过多次寻找答案后,我求助于你.提前致谢.
这个自我回答的问题旨在系统地概述 PowerShell CLI(命令行界面),适用于Windows PowerShell ( powershell.exe) 和PowerShell (Core) v6+(pwsh.exe在 Windows 上,pwsh在 Unix 上)。
尽管存在官方帮助主题(请参阅答案中的链接),但它们并没有描绘出完整的画面并且缺乏系统的处理(截至撰写本文时)。
其中,回答了以下问题:
特定于版本的 CLI 有何不同?
如何将要执行的 PowerShell 代码传递给 CLI?怎么办-Command(-c)和-File(-f)有什么区别?
传递给这些参数的参数需要如何引用和转义?
哪些字符编码问题会起作用?
PowerShell CLI 如何处理 stdin 输入,它们生成什么 stdout/stderr 以及以什么格式?
powershell quoting command-line-interface character-encoding io-redirection
我正在尝试在 PowerShell 中执行以下命令,但我不知道如何转义作为 URL 一部分的 & 符号
az rest `
--method GET `
--uri ("https://graph.microsoft.com/v1.0/groups?`$count=true&`$filter=startsWith(displayName,'some+filter+text')&`$select=id,displayName") `
--headers 'Content-Type=application/json'
Run Code Online (Sandbox Code Playgroud)
由于 & 字符用于启动新命令,因此它会破坏 url 并希望执行其余部分。
有没有办法告诉 powershell 不要这样做?
powershell ×8
escaping ×3
batch-file ×2
parameters ×2
quoting ×2
call ×1
cmd ×1
curl ×1
exit-code ×1
http-headers ×1
json ×1
msiexec ×1
process ×1
quotes ×1
r ×1
rest ×1
scripting ×1
spring-mvc ×1
windows ×1
winscp ×1