在GO语言中,无法将JSON作为HTTP POST请求的主体发送。

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

Unable to send JSON as body for HTTP POST request in GO

问题

当我使用以下GO函数进行POST请求时,在服务器端会收到一个"invalid json"的错误。

如果我发送静态的JSON,例如:

var jsonprep = []byte(`{"username":"xyz@gmail.com","password":"xyz123"}`)

它可以正常工作,而不是:

var jsonprep string = "`{username:"+username+",password:"+password+"}`"

以下是函数的代码:

func makeHttpPostReq(url string, username string, password string){
    client := http.Client{}
    var jsonprep string = "`{username:"+username+",password:"+password+"}`"
    var jsonStr = []byte(jsonprep)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("无法连接到服务器。")
    } else {
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println("body=", string(body))
    }
}

希望这可以帮助到你!

英文:

When i make the POST request using the below GO Function. I get a invalid json on the server side.

If i send static json for example

var jsonprep = []byte(`{"username":"xyz@gmail.com","password":"xyz123"}`) 

it does work instead of

var jsonprep string = "`{username:"+username+",password:"+password+"}`"

.

func makeHttpPostReq(url string, username string, password string){

    client := http.Client{}


    var jsonprep string = "`{username:"+username+",password:"+password+"}`"

    var jsonStr = []byte(jsonprep)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		 fmt.Println("Unable to reach the server.")
	} else {
		 body, _ := ioutil.ReadAll(resp.Body)
		 fmt.Println("body=", string(body))
	}

}

答案1

得分: 7

你引用的方式有误:

http://play.golang.org/p/PueWyQ1atq

var jsonprep string = "`{username:"+username+",password:"+password+"}`"

===> `{username:Bob,password:pass}`

你的意思是:

http://play.golang.org/p/LMuwxArf8G

var jsonprep string = `{"username":"`+username+`","password":"`+password+`"}`
===> {"username":"Bob","password":"pass"}
英文:

You've got your quoting wrong:

http://play.golang.org/p/PueWyQ1atq

var jsonprep string = "`{username:"+username+",password:"+password+"}`"

===> `{username:Bob,password:pass}`

You meant:

http://play.golang.org/p/LMuwxArf8G

var jsonprep string = `{"username":"`+username+`","password":"`+password+`"}`
===> {"username":"Bob","password":"pass"}

答案2

得分: 2

如果你使用以下代码:

var jsonprep string = "`{username:"+username+",password:"+password+"}`"

服务器将会得到以下数据:

`{username:your_username,password:yourpassword}`

因为双引号内的反引号``不是原始字符串字面量,所以它是无效的JSON。你可以手动组合JSON字符串:

var jsonprep string = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"
英文:

If you use

var jsonprep string = "`{username:"+username+",password:"+password+"}`" 

the server will get the data like this:

`{username:your_username,password:yourpassword}`

because the string in back quotes `` which is in the double quotes is not raw string literals, of course it's invalid json. you can compose the json string manually:

var jsonprep string = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"

huangapple
  • 本文由 发表于 2015年6月15日 09:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/30836350.html
匿名

发表评论

匿名网友

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

确定