azure - ServiceBus Dequeue 触发消息在 azure 中解码 Golang 自定义处理程序 : message format

标签 azure go azure-functions

您好,我为 Azure 函数创建了一个 golang 自定义处理程序。我有一个队列,其中包含需要出列和处理的消息。但是,当我尝试处理该消息时,我无法解码其内容。我正在按照这个例子 https://learn.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers#examples当然,我将其调整为与 gingonic 一起使用,但我仍然无法解决问题。出列的消息不应该是 JSON 格式并且很容易反序列化/解码吗?似乎是某种我不知道如何使用 golang 处理的转义字符串。欢迎任何帮助。 下面:function.json、代码片段以及负载外观和错误的日志消息。

{
  "bindings": [
    {
      "name": "myitem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "new-mwsgs",
      "connection": "my_connectionstr",
      "accessRights": "listen",
      "dataType": "string"
    }
  ]
}

type InvokeRequest struct {
    Data     map[string]json.RawMessage
    Metadata map[string]interface{}
}
type MyItem strutc{
    ID string `json:"id"`
    IDnum int `json:"id_num"`
    Flags map[string]interface{} `json:"flags"`

}

func  Post(ctx *gin.Context) {
    var req InvokeRequest

    if err := ctx.BindJSON(&req); err != nil {
        logrus.Error(err)
        
    }
    logrus.Info(req.Data["myitem"])

   var myItem MyItem
   if err:= json.Unmarshal([invokeRequest.Data["myitem"], &myItem); err !=nil{
logrus.Error(err)
}

ctx.Status(http.StatusOK)


}

日志

2021-03-26T15:59:04Z   [Error]   time="2021-03-26T15:59:03Z" level=info msg="map[myitem:\"{\\\"id\\\":\\\"605e04c7fd8f28e17f747952\\\",\\\"id_num\\\":666,\\\"flags\\\":{\\\"eligible\\\":true,\\\"nationwide\\\":true,\\\"persistent_\\\":false}}\"]\"" 
2021-03-26T15:59:04Z   [Error]   time="2021-03-26T15:59:03Z" level=error msg="json: cannot unmarshal string into Go value of type MyItem"


最佳答案

这就是我修复它的方法。我在解码之前添加了一个 strconv.Unquote 。现在可以工作了。

myItemStr, _:= strconv.Unquote(invokeRequest.Data["myitem"])
  var myItem MyItem
   if err:= json.Unmarshal([]byte(myItemStr), &myItem); err !=nil{
logrus.Error(err)
}

关于azure - ServiceBus Dequeue 触发消息在 azure 中解码 Golang 自定义处理程序 : message format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66822465/

相关文章:

go - 如何知道 goroutine 是否还存在?

go - 在 Go 中生成 Bash Shell

c# - Azure Function - 此平台不支持 System.Data.SqlClient

azure - 使用 Azure Functions for Microsoft Flow 随机化一系列数字

Azure 资源管理器 (ARM) 模板参数未定义

c# - https ://<myapp>. azurewebsites.net/.auth/login/aad/callback 是什么意思?

azure - 在Azure中,为什么保留基本公共(public)IP比动态基本公共(public)IP便宜?

powershell - 错误获取-AzureKeyVaultSecret : Unable to retrieve service key for ServicePrincipal account

amazon-web-services - 为 Golang Lambda 功能单元测试模拟 AWS API 网关请求和 DynamoDB

azure - 如何使用 Azure 函数 Cosmos DB 触发器更新集合中的文档?