0

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 1

0

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.

    https://trnktms.com/2022/09/29/sitecore-headless-how-to-reuse-layout-service-serialization-in-custom-api-controllers/

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

https://doc.sitecore.com/xp/en/developers/92/sitecore-experience-manager/the-restful-api-for-the-itemservice.html

8
  • Thanks, but I'm getting error while registering controller in DI (error code CS0012) I'm using framework 4.6 with sitecore 10 (headless).
    – Ranjeet
    Dec 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
  • @Ranjeet Let me know if this works for you. 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.
    – Ranjeet
    Dec 21, 2022 at 6:29
  • @Ranjeet Did you check the article that I shared in the comments? Dec 21, 2022 at 7:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.