英文:
Google Form API - Error when Create, Update Item
问题
我尝试使用Google表单API更新我的现有表单。我在请求中填写了位置字段,但仍然收到服务器的错误消息create_item.location.index is invalid or was not provided
。
创建请求代码
func UpdateForm(formId string, form *forms.Form) *forms.Form {
var requestElements []*forms.Request
// 更新表单信息
requestElements = append(requestElements, &forms.Request{
UpdateFormInfo: &forms.UpdateFormInfoRequest{
Info: form.Info,
UpdateMask: "*",
},
})
// 添加项目
for i, item := range form.Items {
element := &forms.Request{
CreateItem: &forms.CreateItemRequest{
Item: item,
Location: &forms.Location{Index: int64(i)},
},
}
requestElements = append(requestElements, element)
}
request := forms.BatchUpdateFormRequest{
IncludeFormInResponse: true,
Requests: requestElements,
}
response, err := formService.Forms.
BatchUpdate(formId, &request).
Context(context.TODO()).
Do()
if err != nil {
panic(err)
}
return response.Form
}
注意:我正在使用form api
v1。
英文:
I try to update my existing form using google form API. I filled location field in request but still received error message from server create_item.location.index is invalid or was not provided
Create Request Code
func UpdateForm(formId string, form *forms.Form) *forms.Form {
var requestElements []*forms.Request
// Update form info
requestElements = append(requestElements, &forms.Request{
UpdateFormInfo: &forms.UpdateFormInfoRequest{
Info: form.Info,
UpdateMask: "*",
},
})
// Add items
for i, item := range form.Items {
element := &forms.Request{
CreateItem: &forms.CreateItemRequest{
Item: item,
Location: &forms.Location{Index: int64(i)},
},
}
requestElements = append(requestElements, element)
}
request := forms.BatchUpdateFormRequest{
IncludeFormInResponse: true,
Requests: requestElements,
}
response, err := formService.Forms.
BatchUpdate(formId, &request).
Context(context.TODO()).
Do()
if err != nil {
panic(err)
}
return response.Form
}
Note: I'm using form api
v1
答案1
得分: 0
最后我找到了问题所在。当向表单中添加新项时,我们必须从索引0
开始,但是在protobuf中,0
是int
的默认值,所以当请求发送时会被忽略。
解决方案:强制发送字段Index
// 添加项
for i, item := range form.Items {
element := &forms.Request{
CreateItem: &forms.CreateItemRequest{
Item: item,
Location: &forms.Location{
Index: int64(i),
ForceSendFields: []string{"Index"},
},
},
}
requestElements = append(requestElements, element)
}
英文:
Finally i found the issue. When add a new item into form we must start with index 0
, but 0
is default value of int
in protobuf so it will be ignore when request send.
Solution: Force send field Index
// Add items
for i, item := range form.Items {
element := &forms.Request{
CreateItem: &forms.CreateItemRequest{
Item: item,
Location: &forms.Location{
Index: int64(i),
ForceSendFields: []string{"Index"},
},
},
}
requestElements = append(requestElements, element)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论