使用Go进行POST请求失败,但使用curl却成功。

huangapple go评论84阅读模式
英文:

POST with Go fails but works with curl

问题

我已经测试了以下的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>
英文:

I've tested the following curl command against my app, and it returns successfully:

curl --data &quot;username=john&amp;password=acwr6414&quot; http://127.0.0.1:5000/api/login

However trying to replicate the above in go has proven quite a challenge, I keep getting a 400 Bad Request error from the server, here's the code:

	type Creds struct {
		Username string `json:&quot;username&quot;`
		Password string `json:&quot;password&quot;`
	}

user := &quot;john&quot;
pass := &quot;acwr6414&quot;

	creds := Creds{Username: user, Password: pass}
	res, err := goreq.Request{
		Method:    &quot;POST&quot;,
		Uri:       &quot;http://127.0.0.1:5000/api/login&quot;,
		Body:      creds,
		ShowDebug: true,
	}.Do()
	fmt.Println(res.Body.ToString())
	fmt.Println(res, err)

I'm using the goreq package, I've tried at least 3 or 4 other packages with no difference. The error I get is:

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 3.2 Final//EN&quot;&gt;
&lt;title&gt;400 Bad Request&lt;/title&gt;
&lt;h1&gt;Bad Request&lt;/h1&gt;
&lt;p&gt;The browser (or proxy) sent a request that this server could not understand.&lt;/p&gt;

答案1

得分: 7

你的Go代码中使用了json格式的请求体,而使用curl时使用了application/x-www-form-urlencoded格式的请求体。

你可以像在curl中那样手动编码字符串:

Body:      "password=acwr6414&user=john",

或者你可以使用url.Values来正确编码请求体:

creds := url.Values{}
creds.Set("user", "john")
creds.Set("password", "acwr6414")

res, err := goreq.Request{
    ContentType: "application/x-www-form-urlencoded",
    Method:      "POST",
    Uri:         "http://127.0.0.1:5000/api/login",
    Body:        creds.Encode(),
    ShowDebug:   true,
}.Do()
英文:

You're sending a json body with your Go code, but an application/x-www-form-urlencoded body with curl.

You can encode the string manually as you've done with curl:

Body:      &quot;password=acwr6414&amp;user=john&quot;,

Or you can use a url.Values to properly encode the body:

creds := url.Values{}
creds.Set(&quot;user&quot;, &quot;john&quot;)
creds.Set(&quot;password&quot;, &quot;acwr6414&quot;)

res, err := goreq.Request{
	ContentType: &quot;application/x-www-form-urlencoded&quot;,
	Method:      &quot;POST&quot;,
	Uri:         &quot;http://127.0.0.1:5000/api/login&quot;,
	Body:        creds.Encode(),
	ShowDebug:   true,
}.Do()

huangapple
  • 本文由 发表于 2017年1月13日 23:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/41638076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定