Kaynağa Gözat

decodeing http message to xml wrapped into function

master
Patrick Peng Sun 8 yıl önce
ebeveyn
işleme
b0553ccff8
3 değiştirilmiş dosya ile 11 ekleme ve 9 silme
  1. +6
    -0
      inMsg.go
  2. +1
    -2
      outMsg_test.go
  3. +4
    -7
      server.go

+ 6
- 0
inMsg.go Dosyayı Görüntüle

@@ -159,3 +159,9 @@ func ReadEventType(s string) string {
xml.Unmarshal([]byte(s), &r)
return r.Event
}

func decryptToXML(raw string) string {
e := ReadEncryptedMsg(raw)
sd := Decode(e.Encrypt)
return sd
}

+ 1
- 2
outMsg_test.go Dosyayı Görüntüle

@@ -5,8 +5,7 @@ import "testing"
func TestBuildTxtMsg(t *testing.T) {
SetupConfig()
s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE")
e := ReadEncryptedMsg(s)
sd := Decode(e.Encrypt)
sd := decryptToXML(s)
h := ReadCommonHeader(sd)
m := ReadTextMsg(sd)
AssertEqual(t, m.Content, "你好", "Message content => wrong")

+ 4
- 7
server.go Dosyayı Görüntüle

@@ -26,8 +26,8 @@ func apiV1Main(w http.ResponseWriter, r *http.Request) {

switch r.Method {
case "POST":
answerWechatPostEcho(w, r)
//answerWechatPost(w, r)
//answerWechatPostEcho(w, r)
answerWechatPost(w, r)
case "GET":
answerInitialAuth(w, r)
default:
@@ -55,11 +55,8 @@ func answerInitialAuth(w http.ResponseWriter, r *http.Request) {
//
func answerWechatPost(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
//fmt.Printf("get body: %s", string(body))
s := ReadEncryptedMsg(string(body))
//fmt.Printf("to decrypt %s", s.Encrypt)
d := Decode(s.Encrypt)
fmt.Printf("decrypt as: %s", d)
d := decryptToXML(string(body))
fmt.Printf("decrypt as: %s\n", d)
h := ReadCommonHeader(d)
reply, _ := BuildTextMsg(h.MsgType, h.FromUserName)
if h.MsgType == "voice" {

Yükleniyor…
İptal
Kaydet