Webhook endpoint returns 400 (C# .NET)

Solved

Webhook endpoint returns 400 (C# .NET)

StefanV
Visitor
3 0 0

I am currently configuring an endpoint to recieve webhooks from shopify and I've hit a wall.

I've developed a test-endpoint that's quite straight forward:

[HttpPost]
[Route("test")]
public IResult RecieveTesthook([FromBody]string payload)
{
    try
    {
        _worker.SavePayload(payload);
        return Results.NoContent();
    }
    catch (Exception ex)
    {
        _worker.SavePayload(ex.Message);
        return Results.NoContent();
    }
}

The test-endpoint is meant to simply log whatever happens and return 204:NoContent regardless.

I made it to have an opportunity to examine the data from the webhooks, however, even if I tell it to just save whatever happens to a file and then return 204:NoContent, it still returns status code 400:BadRequest.

 

This is the setup:
The server is hosted on a containerized NginX.
Nginx then proxies-forwards to a container that hosts the API.

These two services are set up in seperate docker-compose with bridged networks.

 

I'm expecting a return of Status 204:NoContent to Shopify and whatever payload or exception message present saved to a file.

 

Any ideas on what the next step should be?

Accepted Solution (1)

Liam
Shopify Staff
2873 312 818

This is an accepted solution.

HI StefanV,

 

Based on your description, the issue could be related to the configuration of your server or the handling of the request. Here are a few things you can try:

  1. Check the payload format: Ensure your webhook is sending the data in the correct format that your API is expecting. Shopify usually sends data as JSON in the request body. Your method is expecting a string, which may be causing the issue. 

  2. Check the Content-Type: Make sure the Content-Type header of the incoming request is set to application/json.

  3. Check the routing: Ensure that your routing configuration in your application is correct and that your API is reachable at the route you specified.

  4. Check the headers: Shopify includes an HMAC header in each webhook. If your server is rejecting requests without this header, it could be causing the 400 error.

  5. Check the HTTPS and SSL configuration: Shopify requires the endpoint to be HTTPS and have a valid SSL certificate. You might want to check if your Nginx is correctly set up for SSL termination.

  6. Check the server logs: The server logs may give you more information about why the request is being rejected.

Remember, the endpoint should always return a 200 OK HTTP status code if there are no errors processing the webhook. If Shopify doesn't receive this response, it will keep trying to deliver the webhook for 48 hours.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Liam
Shopify Staff
2873 312 818

This is an accepted solution.

HI StefanV,

 

Based on your description, the issue could be related to the configuration of your server or the handling of the request. Here are a few things you can try:

  1. Check the payload format: Ensure your webhook is sending the data in the correct format that your API is expecting. Shopify usually sends data as JSON in the request body. Your method is expecting a string, which may be causing the issue. 

  2. Check the Content-Type: Make sure the Content-Type header of the incoming request is set to application/json.

  3. Check the routing: Ensure that your routing configuration in your application is correct and that your API is reachable at the route you specified.

  4. Check the headers: Shopify includes an HMAC header in each webhook. If your server is rejecting requests without this header, it could be causing the 400 error.

  5. Check the HTTPS and SSL configuration: Shopify requires the endpoint to be HTTPS and have a valid SSL certificate. You might want to check if your Nginx is correctly set up for SSL termination.

  6. Check the server logs: The server logs may give you more information about why the request is being rejected.

Remember, the endpoint should always return a 200 OK HTTP status code if there are no errors processing the webhook. If Shopify doesn't receive this response, it will keep trying to deliver the webhook for 48 hours.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

StefanV
Visitor
3 0 0

I suppose that sometimes we just need another set of eyes or a fresh mind to find the issue. You got it right at No.1. I, for some reason, expected the Json to arrive as a string. I have now corrected the issue and I'm able to recieve the webhooks just fine.

The new input for the controller takes a JsonObject instead of a string.

Thank you!