如何在golang中使用mux和gorm发布身体原始信息?
我对使用gorilla/mux和gorm的方法post有问题,我想在body raw中请求,但当我执行我的过程时,我的代码是错误的,为什么我的代码会出错?我仍然不明白如何使用gorilla/mux和gorm在身体中使用请求,如何在golang中使用mux和gorm制作post表单 我的错误是:如何在golang中使用mux和gorm发布身体原始信息?,go,go-gorm,mux,Go,Go Gorm,Mux,我对使用gorilla/mux和gorm的方法post有问题,我想在body raw中请求,但当我执行我的过程时,我的代码是错误的,为什么我的代码会出错?我仍然不明白如何使用gorilla/mux和gorm在身体中使用请求,如何在golang中使用mux和gorm制作post表单 我的错误是: package main import ( "encoding/json" "fmt" "github.com/gorilla/mux"
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
"log"
"net/http"
"strconv"
"time"
)
type SMSBlast struct {
SequenceID int `gorm:"column:SequenceID;PRIMARY_KEY"`
MobilePhone string `gorm:"column:MobilePhone"`
Output string `gorm:"column:Output"`
WillBeSentDate *time.Time `gorm:"column:WillBeSentDate"`
SentDate *time.Time `gorm:"column:SentDate"`
Status *string `gorm:"column:Status"`
DtmUpd time.Time `gorm:"column:DtmUpd"`
}
func (SMSBlast) TableName() string {
return "SMSBlast2"
}
func insertSMSBlast(w http.ResponseWriter, r *http.Request){
fmt.Println("New Insert Created")
db, err := gorm.Open("mssql", "sqlserver://sa:@localhost:1433?database=CONFINS")
if err != nil{
panic("failed to connect database")
}
defer db.Close()
vars := mux.Vars(r)
//sequenceid := vars["sequenceid"]
mobilephone := vars["mobilephone"]
output := vars["output"]
willbesentdate := vars["willbesentdate"]
sentdate := vars["sentdate"]
status := vars["status"]
var smsblats SMSBlast
json.NewDecoder(r.Body).Decode(&smsblats)
prindata := db.Debug().Raw("EXEC SMSBlast2Procedure ?, ?, ?, ? , ?", mobilephone, output, willbesentdate, sentdate, status).Scan(smsblats)
fmt.Println(prindata)
json.NewEncoder(w).Encode(&smsblats)
}
func handleRequests(){
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/smsblaststestInsert", insertSMSBlast).Methods("POST")
log.Fatal(http.ListenAndServe(":8080",myRouter))
}
func main(){
fmt.Println("SMSBLASTS ORM")
handleRequests()
}
这里没有太多的数据,但似乎您访问了错误的值,并得到了空白字符串作为回报
vars := mux.Vars(r)
mobilephone := vars["mobilephone"]
output := vars["output"]
willbesentdate := vars["willbesentdate"]
sentdate := vars["sentdate"]
status := vars["status"]
Gorilla希望所有这些值都出现在请求路径(/{mobilephone}/{output}/{willbesentdate}
)中。我怀疑情况并非如此,因为您还将请求体读入结构中
删除该部分,不要使用这些值,请使用加载到结构中的值。请使用收到的错误更新您的问题。@DaleBurrell我添加了错误图片错误告诉您问题所在,SP希望
@sentdate
,但未提供该值。您需要仔细检查传入的参数g并确保您正在使用正确的值传递所有必需的参数。@DaleBurrell我已经更新了我的问题,但现在我从正文中发布的值获取空值,如EXEC SMSBlast2Procedure“”,“”,“”,“”,“”,“”,我已经更新了我的问题。确定-这不是数据库问题,所以我正在取消标记SQL。问题是从HTT获取值P