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/

2
  • maybe "CalculatedMeasurement" : [12,12,34,...] consider each item dont should be greather than 255 Commented Sep 13, 2016 at 22:24
  • 3
    Did you ever work this out? Commented Aug 1, 2017 at 15:42

4 Answers 4

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

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).
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"
} 

1 Comment

No bytes are to be converted in base 64.
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

1 Comment

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).
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.