【发布时间】:2017-05-29 00:43:15
【问题描述】:
我已经针对我的应用测试了以下 curl 命令,它成功返回:
curl --data "username=john&password=acwr6414" http://127.0.0.1:5000/api/login
然而,尝试在 go 中复制上述内容已被证明是一个相当大的挑战,我不断收到来自服务器的 400 Bad Request 错误,代码如下:
type Creds struct {
Username string `json:"username"`
Password string `json:"password"`
}
user := "john"
pass := "acwr6414"
creds := Creds{Username: user, Password: pass}
res, err := goreq.Request{
Method: "POST",
Uri: "http://127.0.0.1:5000/api/login",
Body: creds,
ShowDebug: true,
}.Do()
fmt.Println(res.Body.ToString())
fmt.Println(res, err)
我正在使用 goreq 包,我已经尝试了至少 3 或 4 个其他包,没有任何区别。我得到的错误是:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
【问题讨论】: