November Happy Hour will be moved to Thursday December 5th.

Dylan Barter
Sep 6, 2023
  730
(0 votes)

Optimizely Configured Commerce Custom POST API

Introduction

When creating custom API controllers for an Optimizely B2B project it’s possible you’ll want to create POST calls. Following the architecture of the base code you will likely have a controller that inherits from the BaseApiController and utilizes ExecuteAsync or Execute methods to map your parameters and results and run a service method.

What can sometimes go unnoticed is that any result you’d like to return from a POST call requires a populated “Uri” string field on your result model.  This is because the Optimizely BaseApiController will not return the API result model with the POST response if the model doesn’t have a Uri value or it is null or blank. See the code section below from the BaseApiController Execute method:

            if (this.Request.Method == HttpMethod.Post)
            {
                if (apiResult.Uri.IsBlank())
                {
                    return this.StatusCode(HttpStatusCode.Created);
                }

                return this.Created(apiResult.Uri, apiResult);
            }

You can see that without a Uri parameter on the apiResult object, the method will simply return a “Created” status code as the result and omit the actual apiResult object payload.  So, if you are seeing empty results from your custom POST method, it’s likely you have not properly set the Uri field and the object is being left off the response.  If your response model inherits from BaseModel, then it already has a Uri parameter and you simply need to set it, likely in your mapper.  Several of the Optimizely B2B API flows will set the Uri parameter in the mapper, like the AccountsV1 endpoint does in GetAccountMapper:

        public virtual AccountModel MapResult(
            AddAccountResult serviceResult,
            HttpRequestMessage request
        )
        {
            var accountModel = this.GetAccountMapper.MapResult(
                serviceResult.GetAccountResult,
                request
            );

            accountModel.Password = accountModel.IsGuest ? serviceResult.Password : string.Empty;
            accountModel.BillToId = serviceResult.BillTo.Id;
            accountModel.ShipToId = serviceResult.ShipTo.Id;

            accountModel.Uri = this.UrlHelper.Link(
                "AccountV1",
                new { accountid = Constants.WebApiCurrent },
                request
            );

            return accountModel;
        }

The Uri should accurately identify the way that object could be resourced.  In this case, the UrlHelper.Link method returns the Get Account method which includes the Account Id. 

Sep 06, 2023

Comments

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog