HTTP response code 500 Internal Server Error - golang azure functions

2023-01-10T13:20:27.057+00:00

I developed an api that only receives the POST method using the Golang (GO) language when deploying the application through VSCode everything goes very well, and the api works locally along with my tests.

but when testing using the url that the azure function provides i get an error error:

HTTP response code 500 Internal Server Error

i am very new to the programming world and also to the azure world so i will share my code to get some help

code go:

func main() {
    dbload := Init()
    h := OpenDB(dbload)

    /*port := os.Getenv("HTTP_PLATFORM_PORT")
    if port == "" {
        port = "8080"
    }*/

    router := gin.Default()
    router.POST("/webhook/sendgrid", h.sendgridWeb)
    //router.GET("/ping", ping)
    port := os.Getenv("HTTP_PLATFORM_PORT")

    // default back to 8080 for local dev
    if port == "" {
        port = "8080"
    }

    router.Run("127.0.0.1:" + port)

}

functions.json:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "customHandler": {
    "description": {
      "defaultExecutablePath": "main.exe",
      "workingDirectory": "",
      "arguments": []
    }, 
    "enableForwardingHttpRequest": true
  }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "custom"
  }
}

I did a test using the azure platform, inside my azure function I went to: functions -> name of my function ->> code+test ->> test/run

and I got the following error:

 [Error]   Executed 'Functions.webhook2' (Failed, Id=1322bd2d-5c9d-4b79-9424-2b29b53c2148, Duration=37ms)

I viewed the logs using Kudu, all the logs say:

2023-01-10T13:07:22.900 [Information] Executing 'Functions.webhook2' (Reason='This function was programmatically called via the host APIs.', Id=cdaa0875-9625-414f-86a9-f6dfab4f966a)
2023-01-10T13:07:22.980 [Error] Executed 'Functions.webhook2' (Failed, Id=cdaa0875-9625-414f-86a9-f6dfab4f966a, Duration=31ms)
An existing connection was forcibly closed by the remote host.

a diagnostic was also made to solve problems, but no error is returned

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,937 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2023-01-20T06:17:08.51+00:00

    First I would suggest you add try catch block, exception handling and some additional logging in your code to get insights.

    Second, we see the below error message in the logs

    "An existing connection was forcibly closed by the remote host."

    It is possible that the sendgrid has network restrictions and it is blocking the function's http request. Please check the sendgrid configuration to allow the function request.

    Feel free to reach out to me if you have any queries or concerns.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.