Как разместить сырое тело, используя mux и gorm в golang? - PullRequest
0 голосов
/ 21 февраля 2019

У меня проблема с методом post, использующим gorilla / mux и gorm, я хочу запросить текст в raw, но мой код - это ошибка, когда я ВЫПОЛНЯЮ процедуру, почему моя ошибка кода?Я до сих пор не понимаю, как использовать запрос в теле, используя gorilla / mux и gorm, как сделать пост-форму, используя mux и gorm в golang?

Моя ошибка: enter image description here

    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()
    }

1 Ответ

0 голосов
/ 21 февраля 2019

Здесь не так много данных, но кажется, что вы получаете неправильные значения и получаете пустые строки взамен.

    vars := mux.Vars(r)
    mobilephone := vars["mobilephone"]
    output := vars["output"]
    willbesentdate := vars["willbesentdate"]
    sentdate := vars["sentdate"]
    status := vars["status"]

Gorilla ожидает, что все онибыть значения, присутствующие в пути запроса (/{mobilephone}/{output}/{willbesentdate}. Я подозреваю, что это не так, поскольку вы также читаете в теле запроса в структуре.

Удалите эту часть и не используйте эти значения, используйтезначения, которые загружаются в вашу структуру.

...