Using the 4.0.40 version, I have this as a Service:
public object Post(ShopInformation request)
{
var result = this.Validator.Validate(request);
if (!result.IsValid)
throw result.ToException();
return AddNewShop(request);
}
where ShopInformation
is
[Api("Shop Service")]
[ApiResponse(HttpStatusCode.BadRequest, "Your request was not understood")]
[ApiResponse(HttpStatusCode.InternalServerError, "Oops, something broke")]
[Route("/shops/{AccountId}/{ContactId}", "POST", Summary = "Create a new shop to the current account")]
[Route("/shops/{AccountId}/{ContactId}", "PUT", Summary = "Update a shop from the current account")]
[Route("/shops/", "POST", Summary = "Create a new shop to the current account")]
[Route("/shops/", "PUT", Summary = "Update a shop from the current account")]
[Authenticate()]
public class ShopInformation : IReturn<ShopWithImagesResponse>
{
public ShopInformation() { }
[ApiMember(Description = "Contact ID of the user making the request",
DataType = "int", IsRequired = true)]
public int ContactId { get; set; }
[ApiMember(Description = "Account ID of the user making the request",
DataType = "int", IsRequired = true)]
public int AccountId { get; set; }
[ApiMember(Description = "Shop information for creation or edition",
IsRequired = false)]
public ShopResponse ShopInfo { get; set; }
[ApiMember(Description = "Images for uploading", IsRequired = false)]
public ShopFiles Files { get; set; }
}
and now POSTing through Postman:
with Text
:
http://s6.postimg.org/equ9lke8x/screenshot_15.png
with Form fields
:
http://s6.postimg.org/j2ivakl5t/screenshot_16.png
with application/json
:
The error does not popup for me as I’m using the ServiceStack.Client
in a .NET demo app, but for those working with the API using a different language, like PHP for example, this took me a while to figure out why they couldn’t do a normal POST with data…
Is there a better way to avoid throwing a Object reference not set to an instance of an object
when the input data type mismatch?