在 .NETCore 服务器上调用 REST API

Calling a REST API on a .NETCore server

我不是 linux 大师,使用 curl 对我来说太复杂了。因此,我找到了 httpie,并尝试在 CentOS 7 下的 Docker 上针对 .NETCore 2 和 运行 调用我自己用 C# 编写的 REST 服务器之一。

我在 Windows 上有一个有效的 Powershell 脚本,并尝试使用 httpie 将其移植到 bash 脚本,不幸的是没有成功。

API 在服务器上生成一个文件,returns 它作为 octet-stream。服务器上的headers定义如下:

        Options = new Dictionary<string, string>
        {
            {"Content-Type", "application/octet-stream"},
            {"X-Api-DocType", filetype},
            {"X-Api-DocLength", responseStream.Length.ToString()}
        };

我的 POWERSHELL 脚本(工作正常)如下所示:

$request = @{
    Namespace = "MyApplication.MessagingApi"
    Version = "current"
    DataDefinitionLanguage = "Csharp"
    MakePartial = $false
    MakeVirtual = $false
}
$docPath = "D:\Projects\AccountingApiMessageDefinitions.cs"
$Url = "http://172.16.63.241:6083/bbopman/messaging/apireferences"
$response = Invoke-WebRequest -Method Get -Uri $Url -Body $request
Set-Content -Path $docPath -Encoding Byte -Value $response.Content

我将其移植到 bash 脚本如下:

#!/bin/bash
url="http://172.16.63.241:6083/bbopman/messaging/apireferences"
request="Namespace=='MyApplication.MessagingApi' Version=='current' DataDefinitionLanguage=='Csharp'"
http -d GET $url $request > ~/AccountingApiMessageDefinitions.cs

我返回的是 BAD REQUEST,但在服务器上成功处理了调用。所以我想我在阅读回复时犯了一些错误,但不知道是什么....这是我 shell:

中的输出
$ ./ExecuteRestCall.sh 
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Wed, 30 May 2018 18:45:50 GMT
Server: Kestrel
Transfer-Encoding: chunked
Vary: Accept
X-Powered-By: ServiceStack/5.02 NETStandard/.NET

谁能告诉我如何从 Linux shell 中正确调用它?非常感谢。

httpie 的参数值不需要引号。发送时不要加引号。

request="Namespace==MyApplication.MessagingApi Version==current DataDefinitionLanguage==Csharp"

此外,您似乎缺少 shell 脚本版本中的 MakePartialMakeVirtual 参数。

顺便说一句:"on the server the call is successfully processed" - 这不可能是真的。错误的请求状态是由服务器返回的,而不是客户端发生的事情。