cs0815
cs0815

Reputation: 17418

fiddler post + webapi 2 issue

This is my webapi 2 endpoint - the MVC duplicate suggestion is not relevant

[Route("Test2")]
[HttpPost]
public IHttpActionResult Test2([FromBody] Guid? guid)
{
    return Ok();
}

when I use fiddler to manually test this using:

Content-Type: application/json

in the header and this payload in the body:

{"guid":"1c3c8edc-d87a-46dc-adbf-e7112bf16d22"}

The method is hit but the guid is null. Any ideas?

Upvotes: 1

Views: 173

Answers (2)

Anik Saha
Anik Saha

Reputation: 4494

You send response through header. Thats why you get null. You have to send request through body.

public class Test
{
 public Guid guid {get; set;}
}

you have to sent request through body like

"1c3c8edc-d87a-46dc-adbf-e7112bf16d22"

and if you want to send request through header then your code will be like this

[Route("Test2")]
[HttpPost]
public IHttpActionResult Test2()
{
   IEnumerable<string> headerValues=request.Headers.GetValues("MyCustomID");
   var guid = headerValues.FirstOrDefault();
   return Ok();
}

Upvotes: 1

marcinax
marcinax

Reputation: 1195

It can't be deserialized directly to Guid. Now, you are sending object from fiddler, something like:

public class SampleObject
{
     public Guid guid {get; set;}
}

Try send just:

"1c3c8edc-d87a-46dc-adbf-e7112bf16d22"

in the body of the request.

Upvotes: 2

Related Questions