Solved

Getting a webhook to return JSon data back to the caller

  • 10 June 2022
  • 3 replies
  • 43 views

Userlevel 4
Badge

I have a webhook working in Acumatica. I want to be able to return, to the caller, some JSon of the transaction that they created instead of just status 200.

public async Task<System.Web.Http.IHttpActionResult> ProcessRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
//do some things
}
catch (Exception ex)
{
var failed = new ExceptionResult(ex, true, new DefaultContentNegotiator(), request, new[] { new JsonMediaTypeFormatter() });
return failed;
}

//This is where I’m stuck. I’d be happy to return some JSon, similar to what the API would return after a successful call

return ???
}

Intellisense is good at telling me what I can’t do, not really good at telling what I should do. :)

icon

Best answer by Marco Villasenor 10 June 2022, 18:41

View original

3 replies

Userlevel 2
Badge

I your webhook is already working then you can use the Ok class to send a 200 response with some content, like this:

return new Ok(new { 
OrderType = new { value = "EO" },
OrderNbr = new { value = "001979" },
...etc...
});

The JSON returned by the API is easy to reproduce if you are familiar with the entity you wish to send, but if it’s not required you can simplify the response because the REST API uses nested objects for each value as illustrated in the code above.

I posted a webhook example in github which you can find here: https://github.com/markoan/acumatica-webhook-example/blob/master/WebhookDemo/WebhookExample.cs

It returns just an empty 200 response, but you can easily modify it to return an object.
 

Userlevel 4
Badge

Thank you Marco, both for your response and for the example in github - that’s what I used to create my webhook.

I think I’m hitting a C# challenge because I’m not seeing how to create the Ok object. It is part of the MVC Core - should I pull that into my project?

Userlevel 2
Badge

My bad, I linked the newest version of the class but in my code I actually used a different library.

My example, and I suppose your code, uses the System.Web.Http library. That version exposes the JsonResult class. You can use it like this:

// If you don't see that class, try adding: using System.Web.Http.Results;
// to the top of your file.

return new Json(new {
OrderType = new { value = "EO" },
OrderNbr = new { value = "001979" },
...etc...
});

 

Sorry for the confusion. Acumatica is migrating to Core so this will still be useful in the future.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2022  Acumatica, Inc. All rights reserved