Powershell 使用十六进制值调用WebRequest

Powershell 使用十六进制值调用WebRequest,powershell,curl,Powershell,Curl,我试图卷曲一个包含分号的有效载荷,但它失败了 Invoke-WebRequest -Uri $uri -Body '{"text" : "foo;"}' -Method Post Invoke-WebRequest: {"id":"model.incoming_hook.parse_data.app_error","message":"Unable to parse incoming

我试图卷曲一个包含分号的有效载荷,但它失败了

Invoke-WebRequest -Uri $uri -Body '{"text" : "foo;"}' -Method Post

Invoke-WebRequest: {"id":"model.incoming_hook.parse_data.app_error","message":"Unable to parse incoming data","detailed_error":"","request_id":"3papdgjsni87untfo7e3rsogpy","status_code":400}
但是

很好。我已将分号转换为%3B,这似乎是发布为;。我的问题是它将所有%2F字符串转换为斜杠,然后斜杠断开(出于某种原因,它必须是文本2F值)


当使用curl命令或在python中使用curl命令时,这是正常的,因此端点似乎不接受“;”价值我尝试过单引号、双引号、转换为json、斜杠等,但似乎没有任何效果。

要将内容中的所有信息汇集在一起,有几个选项,具体取决于服务器接受的内容类型

内容类型:应用程序/json

如果它接受
内容类型:application/json
,您可以在注释中使用@Jeroen Mostert的答案:

#哈希表中的原始文本
$data=@{“text”=“foo;%2F”};
#转换为json
$json=converttoJSON-InputObject$data-Compress;
#发布为“应用程序/json”
调用WebRequest-Uri“http://example.org“-Body$json-Method“POST”-ContentType“application/json”;
将发送以下HTTP请求:

POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/json
Host: example.org
Content-Length: 18
Connection: Keep-Alive

{"text":"foo;%2F"}
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 36
Connection: Keep-Alive

%7B%22text%22%3A%22foo%3B%252F%22%7D
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 22
Connection: Keep-Alive

{"text":"foo%3B%252F"}
内容类型:应用程序/x-www-form-urlencoded

默认情况下,PowerShell将使用
内容类型:application/x-www-form-urlencoded
发布数据,因此您可以按如下方式发送文本:

#哈希表中的原始文本
$data=@{“text”=“foo;%2F”};
#转换为json
$json=converttoJSON-InputObject$data-Compress;
#url编码
$body=[System.Net.WebUtility]::UrlEncode($json);
#发布为“application/x-www-form-urlencoded”
调用WebRequest-Uri“http://example.org“-Body$Body-方法“POST”;
将发送以下HTTP请求:

POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/json
Host: example.org
Content-Length: 18
Connection: Keep-Alive

{"text":"foo;%2F"}
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 36
Connection: Keep-Alive

%7B%22text%22%3A%22foo%3B%252F%22%7D
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 22
Connection: Keep-Alive

{"text":"foo%3B%252F"}
注意-完整的json文本是url编码的,但这可能不是服务器所期望的

自定义编码

如果出于某种原因,您的服务器需要原始json语法,但
text
属性的内容需要进行url编码,您可以执行如下自定义编码:

#哈希表中的原始文本
$data=@{
“text”=“foo;%2F”
};
#url编码文本
$data.text=[System.Net.WebUtility]::UrlEncode($data.text);
#转换为json
$json=converttoJSON-InputObject$data-Compress;
#发布为“application/x-www-form-urlencoded”
调用WebRequest-Uri“http://example.org“-Body$json-方法“POST”;
这将生成HTTP请求:

POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/json
Host: example.org
Content-Length: 18
Connection: Keep-Alive

{"text":"foo;%2F"}
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 36
Connection: Keep-Alive

%7B%22text%22%3A%22foo%3B%252F%22%7D
POST http://example.org/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.1
Content-Type: application/x-www-form-urlencoded
Host: example.org
Content-Length: 22
Connection: Keep-Alive

{"text":"foo%3B%252F"}

希望其中一个对你有用。如果没有,上面应该有足够多的代码片段,可以将这些代码放在一起:-)。

试过了吗?它更加灵活,因为它面向API的使用。值得注意的是,它允许您使用哈希表,而不是手动构建JSON,这有助于可读性。同样的错误:
Invoke RestMethod:{“id”:“model.incoming_hook.parse_data.app_error”,“message:“无法解析传入数据”,“detaild_error:”,“request_id:“zfuhmx14upniiea55bkn6fg4ke”,“status_code:”400}
My bad,
Invoke RestMethod
似乎也不会自动转换为JSON。但是,
调用rest方法”http://localhost“-Body(@{text=“foo;”}| ConvertTo Json)-Method Post-ContentType”application/Json“
适用于我,调用RestMethod也适用于我”http://localhost“-Body'{”text:“foo;“}”-Method Post-ContentType“application/json”。我打赌你会被遗漏的
内容类型
绊倒(默认情况下,你会在帖子上看到
应用程序/x-www-form-urlencoded
)。你能在帖子中发布一个带有斜杠的有问题正文的示例吗?如果我这样做:
调用webrequest-uri”http://example.org“-body'{“text”:“foo;/”}'-methodpost
或this:
调用webrequest-uri”http://example.org“-body'{“text”:“foo%3B%2F”}
-method Post它发布文本字符串
{“text”:“foo;/”}
{“text”:“foo%3B%2F”}”
在正文中可能我误解了,但是,您的意思是服务器正在url解码正文,并且您希望解码的正文包含字符串
%2F
?在这种情况下,您可能需要将
%
url编码为
%25
——例如,您的文本字符串
%2F”
变为
%252F”
,因此您解码的字符串在服务器上再次变为
%2F
?如果可以的话,有一些类可以帮助编码,但让我们先检查一下这是否是您想要的。。。