How to send JSON with byte array to web API / postman
Asked Answered
D

4

22

I want to be able to send to both

  1. a Web API
  2. Postman to Web API

I can do simple GET Requests to my Web API with Postman, but what I don't understand is how to send a Byte Array.

With Postman, I know that it is a PUT

This is the Web API signature

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

The Request class

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

Should I be sending a JSON via Raw Data using Postman?

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

I know when the Web page calls the Web API, I do see this in the Inspector

enter image description here

Postman snippet

enter image description here

How do I send data via Postman , and how do I send to a web API http://localhost:42822/api/Manifest/VerifyChain/

Dorran answered 13/9, 2016 at 20:44 Comment(2)
maybe "CalculatedMeasurement" : [12,12,34,...] consider each item dont should be greather than 255Smutchy
Did you ever work this out?Won
W
8

In case you're looking for how to convert the file to a byte array for the postman request:

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

This will result into a long string that looks like this:

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

And then use it in the postman request like so:

{
    "FileBytes":[49,48,58,50,52,58,50,54],
    "DeviceId": 12345
}
Wishful answered 26/5, 2021 at 9:39 Comment(1)
I tried your solution but I got an error: "The JSON value could not be converted to System.Byte[]". What did work was passing a base 64 encoded string in the JSON (instead of passing an array as you're doing above).Kali
C
2

Think your Webapi method needs [HttpPut]

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

and in postman your message body would be an array

{
  "CalculatedMeasurement":[71,107,98],
  "DeviceId": "afdghufsdjdf"
} 
Coarctate answered 2/11, 2018 at 11:36 Comment(1)
No bytes are to be converted in base 64.Apollonian
L
2

You must convert the file to base64 and send it as string.

  1. Your WebAPI method is missing a the PUT indicator
    [HttpPut("api/Manifest/VerifyChain/")]
    [ResponseType(typeof (VerifyManifestChainResponse))]
    public IHttpActionResult PutVerifyManifestChain([FromBody] VerifyManifestChainRequest message)
    {
       //.....
    }
  1. In Postman, I must use the body as JSON

Request in Postman

Lunik answered 22/5, 2020 at 22:53 Comment(1)
This is the correct answer : convert the bytes in base 64 and use the resulting string in the postman request. For example, in java, converting bytes to base 64 is as easy as Base64.encode(myBytes).Apollonian
I
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();
}

this is the way to send the response back as byte[] it can be converted into json format in postman what ever data you sent will be sent as byte[] when you debugg you can see this

Imprimatur answered 15/3 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.