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

问题描述 投票:0回答:4

我希望能够发送给两者 1. 一个Web API 2. Postman 到 Web Api

我可以使用 Postman 对我的 Web Api 执行简单的 GET 请求,但我不明白如何发送字节数组。

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

这是 Web api 签名

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

Request 类

public class VerifyManifestChainRequest
{
    public byte[] CalculatedMeasurement { get; set; }
    public string DeviceId { get; set; }
}

我应该使用 Postman 通过原始数据发送 JSON 吗?

{
   "CalculatedMeasurement": ?????,
   "DeviceId": "00022B9A000000010001"
}

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

邮递员片段

如何通过 Postman 发送数据,以及如何发送到 Web api http://localhost:42822/api/Manifest/VerifyChain/

json api asp.net-web-api postman
4个回答
8
投票

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

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

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

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

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

{
    "FileBytes":[49,48,58,50,52,58,50,54],
    "DeviceId": 12345
}

2
投票

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

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

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

{
  "CalculatedMeasurement":[71,107,98],
  "DeviceId": "afdghufsdjdf"
} 

2
投票

您必须将文件转换为 base64 并将其作为字符串发送。

  1. 您的 WebAPI 方法缺少 PUT 指示符
    [HttpPut("api/Manifest/VerifyChain/")]
    [ResponseType(typeof (VerifyManifestChainResponse))]
    public IHttpActionResult PutVerifyManifestChain([FromBody] VerifyManifestChainRequest message)
    {
       //.....
    }
  1. 在 Postman 中,我必须使用 JSON 格式的正文

Request in Postman


0
投票
@Path("/{restaurantId}")
@GET
@Produces("application/json")
public byte[] getRestaurantById(@PathParam("restaurantId") final long restaurantId) {
    final Restaurant restaurant = restaurantService.getRestaurantById(restaurantId);

    if (null != restaurant) {
        return jsonObject.build(restaurant).asJson();
    }

    return jsonObject.build("Enter A Valid Restaurant Id").asJson();
}

这是将响应以 byte[] 形式发送回来的方法,可以在 postman 中将其转换为 json 格式 当你调试时,你发送的任何数据都将作为 byte[] 发送,你可以看到这个

© www.soinside.com 2019 - 2024. All rights reserved.