I have 3rd party application, and I want to send some data to sitecore application using POST method. So is it possible to handle HTTP POST within sitecore MVC ? If yes please suggest how to proceed
1 Answer
Yes you can do it by creating an API controller
. So for this you need to follow these steps.
Create a controller and inherit it from
ApiController
namespace Feature.ExampleApi.Controllers { public class ExampleApiController : ApiController { [HttpPost] [Route("post/{itemNameString}")] public IHttpActionResult CreateItem(string itemNameString) { // Do your operation // Do sth with item return Json(result); } } }
Register your controller
namespace Feature.ExampleApi { public class ServicesConfigurator : IServicesConfigurator { public void Configure(IServiceCollection serviceCollection) { // Controllers serviceCollection.Replace(ServiceDescriptor.Transient(typeof(ExampleApiController), typeof(ExampleApiController))); } } }
If you are working with headless environment, register it like this.
public void Configure(IServiceCollection serviceCollection) { serviceCollection.AddScoped<IDefaultRenderingContentsResolver, DefaultRenderingContentsResolver>(); serviceCollection.AddMvcControllers("*.Feature"); }
Follow this article for headless environment.
Create config.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <services> <configurator type="Feature.ExampleApi.ServicesConfigurator, Feature.ExampleApi" /> </services> </sitecore> </configuration>
Follow this article for more details.
https://jakubwajs.wordpress.com/2020/06/21/create-api-controller-in-sitecore-9-3/
You can also work with the Sitecore Item Serice to create items and more.
-
Thanks, but I'm getting error while registering controller in DI (error code CS0012) I'm using framework 4.6 with sitecore 10 (headless).– RanjeetDec 20, 2022 at 13:21
-
@Ranjeet If you are working with headless, you can try this approach. trnktms.com/2022/09/29/… Dec 20, 2022 at 14:54
-
-
yes I'm working with headless. And i want to use HTTP POST api method within sitecore, so that i can expose api to 3rd party and get data from 3rd party tool via Sitecore api. As I'm new in sitecore so please help me out with an example.– RanjeetDec 21, 2022 at 6:29
-
@Ranjeet Did you check the article that I shared in the comments? Dec 21, 2022 at 7:09