Home >Backend Development >Golang >AWS lambda golang iotdataplane.PublishInput() payload is always empty

AWS lambda golang iotdataplane.PublishInput() payload is always empty

WBOY
WBOYforward
2024-02-11 19:10:08516browse

AWS lambda golang iotdataplane.PublishInput() 有效负载始终为空

php editor Xinyi reminds everyone that when using the AWS Lambda Golang programming language, there is one thing to note: when using the iotdataplane.PublishInput() method, its payload (Payload) Always empty. This means that when writing relevant code, special attention needs to be paid to handling empty load situations to ensure the normal operation of the program. In the actual development process, conditional statements or error handling mechanisms can be used to handle this situation to avoid potential problems. Keeping this in mind will help improve the robustness and reliability of your code.

Question content

I cannot understand how to use Golang to package the Payload field of iotdataplane.PublishInput(). Can anyone help? My Lambda publishes without errors, but the payload always shows up as empty in CloudWatch and subscribers. This is what I'm doing:

type Response struct {
    SerialNum string `json:"serial_number"`
    Time      int64  `json:"time"`
}

resp := Response{
    SerialNum: sernum,
    Time: utc,
}
payload, _ :=- json.Marshal(resp)  // payload is a byte array

...

params := &iotdataplane.PublishInput{
    Topic: &topic,
    Payload: payload,
    PayloadFormatIndicator: aws.String(iotdataplane.PayloadFormatIndicatorUtf8Data),
    Qos: aws.Int64(0),
}

result, err := svc_client.Publish(params)

...

I've tried various variations of the payload without making any progress. It was also thought at one point that the payload needed to be a struct with "locationName" and "type" as fields, but that didn't work out either. My payload byte array is definitely not empty.

Solution

Found my problem.

I'm using Mosquito to test my lambda responses and the problem is not with the lambda function but with the Mosquito subscriber entries used for testing. I'm using the same id (-i option) as my Mosquito bar, which I think confuses the broker. When I removed the -i option from the subscription entry, the lambda publishing started working.

The above is the detailed content of AWS lambda golang iotdataplane.PublishInput() payload is always empty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete