ソースを参照

protect < > & for json message, prevent it from being encoded.

master
Patrick Peng Sun 8年前
コミット
39d70527ae
2個のファイルの変更15行の追加4行の削除
  1. +12
    -1
      kfsend.go
  2. +3
    -3
      kfsend_test.go

+ 12
- 1
kfsend.go ファイルの表示

package main package main


import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
s.Text.Content = txt s.Text.Content = txt
s.ToUser = user s.ToUser = user


j, _ := json.Marshal(s)
//j, _ := json.Marshal(s) // it will escape html < > and & to \u0006
j, _ := myJSONMarshal(s) // this reserves < > and &
log.Println(string(j))
postJSON(j, u) postJSON(j, u)
} }


func myJSONMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}

type sendPicMsg struct { type sendPicMsg struct {
ToUser string `json:"touser"` ToUser string `json:"touser"`
MsgType string `json:"msgtype"` MsgType string `json:"msgtype"`

+ 3
- 3
kfsend_test.go ファイルの表示



func TestSendTxt(t *testing.T) { func TestSendTxt(t *testing.T) {
SetupConfig() SetupConfig()
msg := fmt.Sprintf("测试消息, %s ", time.Now().String())
msg := fmt.Sprintf("测试消息 & < >, %s ", time.Now().String())


randinit()
msg = RandStringRunes(2048)
// randinit()
// msg = RandStringRunes(2048)


kfSendTxt(toUser, msg) kfSendTxt(toUser, msg)
} }

読み込み中…
キャンセル
保存