我有以下用于测试 http 请求的代码:
func TestAuthenticate(t *testing.T) {
api := &ApiResource{}
ws := new(restful.WebService)
ws.Consumes(restful.MIME_JSON, restful.MIME_XML)
ws.Produces(restful.MIME_JSON, restful.MIME_JSON)
ws.Route(ws.POST("/login").To(api.Authenticate))
restful.Add(ws)
bodyReader := strings.NewReader("<request><Username>42</Username><Password>adasddsa</Password><Channel>M</Channel></request>")
httpRequest, _ := http.NewRequest("POST", "/login", bodyReader)
// httpRequest.Header.Set("Content-Type", restful.MIME_JSON)
httpRequest.Header.Set("Content-Type", restful.MIME_XML)
httpWriter := httptest.NewRecorder()
restful.DefaultContainer.ServeHTTP(httpWriter, httpRequest)
}
我尝试将 json 用作具有相同字符串的字符串,NewReader并尝试将 struct 与json.Marshal.
它们都不起作用。
有没有一种方法可以为 编码bodyReader一个有效的第三个参数http.NewRequest?
与NewReaderJSON 中的输入类似的请求是:
bodyReader := strings.NewReader("{'Username': '12124', 'Password': 'testinasg', 'Channel': 'M'}")
结构字段是: Username, Password, Channel
ABOUTYOU
相关分类