Developer forum

Forum » Dynamicweb 10 » Issue building my own APiController

Issue building my own APiController

Jan Sangill
Reply

Hi,
I am trying to build my own API Controller in DW10 - since I am porting I am hitting the DW9 to DW10.
No matter what I try, I get an "HTTP ERROR 400".
I am hitting my method: "/api/test/get" - but always the error above. I am not sure why or how to fix it.

I have attached two simplified screenshots where it also gives the same error. Can anyone let me know what I am doing wrong?

 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Are you testing it in a browser? If so, switch to a tool like Postman or Bruno. I think you're getting an error because the browser sends an Accept header (like: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7) by default. DynamicWeb then doesn't know what to return and returns a BadRequest instead.

In a tool like Bruno there's no default Accept header which will make your request succeed. You can also be explicit and send application/json as the Accept header.

Hope this helps,

Imar

Votes for this answer: 1
 
Jan Sangill
Reply

Hi Imar, I am almost a shamed now:) I did try with postman, but had another error that threw me off there. I just assumed it should also work in the browser too. To much time wated here:>
Thought it was code related.
Thank you.:)

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

:-) It threw me off a bit as well; most APIs I work with return JSON in the browser also.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi

We have been looking into this.

This is due to a new feature for the /dwapi that has recently rolled out so that /dwapi can be used easier with tools like htmx.

What that feature does is that if you request "accept: text/html" together with "x-dw-template: MyModule/item.cshtml", the response object will be serialized to html using the passed template.

The problem is that if you "accept: text/html" but no x-dw-template, you get a bad request. We are now looking at to solve that so if x-dw-template is missing, it will serialize to json.

There is a workaround to disable the behavior by setting RespectBrowserAcceptHeader = False (which is default by MVC and Dynamicweb sets it to true)

using Dynamicweb.Host.Core;
using Microsoft.AspNetCore.Mvc;
 
public class PipelineExtensions : IPipeline
{
    public int Rank => 200;
    public void RegisterApplicationComponents(IApplicationBuilder app)
    {
        app.MapWhen(isMyApiPath, app =>
        {
            app.UseRouting();
 
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        });
 
        static bool isMyApiPath(HttpContext context) => context.Request.Path.StartsWithSegments("/myapi", StringComparison.OrdinalIgnoreCase);
    }
 
    public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
    {
        services.Configure<MvcOptions>(options =>
        {
            options.RespectBrowserAcceptHeader = false;
        });
    }
 
    public void RunInitializers()
    {
    }
}
 
using Microsoft.AspNetCore.Mvc;
 
[ApiController]
[Route("myapi/test")]
public class MyApiController : ControllerBase
{
    public class GreetingResponse
    {
        public string? Message { get; set; }
    }
 
    [HttpGet, Route("hi")]
    public IActionResult Hi([FromQuery] string name = "World")
    {
        var response = new GreetingResponse
        {
            Message = $"Hello {name}!"
        };
        return Ok(response);
    }
}

 

You must be logged in to post in the forum