Amazon web services 在AWS Lambda上与golang解组错误,在本地工作
我在golang中创建了支持端点API的代码(通过get查询)。这是API端点的文档: 代码如下所示:Amazon web services 在AWS Lambda上与golang解组错误,在本地工作,amazon-web-services,go,aws-lambda,Amazon Web Services,Go,Aws Lambda,我在golang中创建了支持端点API的代码(通过get查询)。这是API端点的文档: 代码如下所示: type Campaign struct { Id int `json:"id,omitempty"` Name string `json:"name,omitempty"` Subject string `json:"subject,omitempty"` FromName string `json:"fr
type Campaign struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Subject string `json:"subject,omitempty"`
FromName string `json:"fromName,omitempty"`
FromAddress struct {
Id int `json:"id,omitempty"`
Email string `json:"email,omitempty"`
}
HtmlContent string `json:"htmlContent,omitempty"`
PlainTextContent string `json:"plainTextContent,omitempty"`
ReplyAction string `json:"replyAction,omitempty"`
IsSplitTest bool `json:"isSplitTest,omitempty"`
Status string `json:"status,omitempty"`
}
func (dcfg DotmailerApiConfig) GetContacts2() ([]*dotmailermodels.Contact) {
var (
allContacts, respContacts []*dotmailermodels.Contact
selected = 1000
skip = 0
err error
)
for true {
url := dcfg.Url + fmt.Sprintf("v2/contacts?withFullData=%s&select=%s&skip=%s",
strconv.FormatBool(false),
strconv.Itoa(selected),
strconv.Itoa(skip))
resp := dcfg.GetRequesDotmailertBuilder(url)
err = json.Unmarshal(resp, &respContacts)
if err != nil {
Error.Println(err) // just error trace
}
allContacts = append(allContacts, respContacts...)
if len(respContacts) == 1000 {
skip += 1000
respContacts = nil
continue
}
break
}
return allContacts
}
当我在电脑上运行时,我得到了正确的响应。当我在Lambda中使用它时,我得到以下错误:
[ERROR] 2019/03/24 18:37:26 dotmailergetrequests.go:110: json: cannot unmarshal object into Go value of type []*dotmailermodels.Campaign
你知道为什么吗?试试这个:
type Address struct {
Id int `json:"id,omitempty"`
Email string `json:"email,omitempty"`
}
type Campaign struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Subject string `json:"subject,omitempty"`
FromName string `json:"fromName,omitempty"`
FromAddress *Address `json:"fromAddress,omitempty"`
HtmlContent string `json:"htmlContent,omitempty"`
PlainTextContent string `json:"plainTextContent,omitempty"`
ReplyAction string `json:"replyAction,omitempty"`
IsSplitTest bool `json:"isSplitTest,omitempty"`
Status string `json:"status,omitempty"`
}
我发现了那个错误。除了在另一个文件中导入密码之外,一切都是正确的。消息表明根JSON值是一个对象,但应用程序将解组到一个片段。要调试此问题,请打印您试图解组的JSON文本。随机猜测是API返回的是错误,而不是应用程序预期的错误。打印HTTP响应状态代码以帮助调试这种可能性。但它在我的电脑上运行良好。是的,这两种环境之间有些不同。应用程序似乎在这两个环境中接收到不同的API响应。在失败的环境中打印HTTP响应状态代码和JSON文本可能会提示出问题所在。与您的问题无关,但没有理由调用
strconv.*
以获取fmt.Sprintf
的参数。只需使用适当的格式化动词即可——Printf/Sprintf的全部目的就是为您进行格式化。