如何将带有字节数组的json发送到web api/postman

12 api json asp.net-web-api postman

我希望能够发送给两者1. Web Api 2. Postman to Web Api

我可以对Web Api进行简单的GET调用并使用Postman,但我不理解的是能够发送字节数组.

有了Postman,我知道这是一个PUT

这是Web api签名

[Route("api/Manifest/VerifyChain/")]
[ResponseType(typeof (VerifyManifestChainResponse))]
public IHttpActionResult PutVerifyManifestChain([FromBody] VerifyManifestChainRequest message)
{
   //.....
}
Run Code Online (Sandbox Code Playgroud)

请求类

public class VerifyManifestChainRequest
{
    public byte[] CalculatedMeasurement { get; set; }
    public string DeviceId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我应该使用Postman在Body中以原始格式发送JSON吗?

{
   "CalculatedMeasurement": ?????,
   "DeviceId": "00022B9A000000010001"
}
Run Code Online (Sandbox Code Playgroud)

我知道当Web页面调用Web Api时,我确实在Watch中看到了这一点

在此输入图像描述

邮差摘要

在此输入图像描述

我如何通过Postman发送数据,也很高兴知道如何发送到web api http:// localhost:42822/api/Manifest/VerifyChain /

Mil*_*ana 8

如果您正在寻找如何将文件转换为字节数组以供邮递员请求:

byte[] bytes = System.IO.File.ReadAllBytes(@"C:\temp\myFile.txt");
string bytesStr = string.Join(",", bytes);
Run Code Online (Sandbox Code Playgroud)

这将产生一个长字符串,如下所示:

"49,48,58,50,52,58,50,54,..."

然后在邮递员请求中使用它,如下所示:

{
    "FileBytes":[49,48,58,50,52,58,50,54],
    "DeviceId": 12345
}
Run Code Online (Sandbox Code Playgroud)


mat*_*gic 2

认为您的 Webapi 方法需要 [HttpPut]

[HttpPut]
[Route("api/Manifest/VerifyChain/")]
[ResponseType(typeof (VerifyManifestChainResponse))]
public IHttpActionResult PutVerifyManifestChain([FromBody] VerifyManifestChainRequest message)
{
   //.....
}
Run Code Online (Sandbox Code Playgroud)

在邮递员中,你的消息正文将是一个数组

{
  "CalculatedMeasurement":[71,107,98],
  "DeviceId": "afdghufsdjdf"
} 
Run Code Online (Sandbox Code Playgroud)