json - Golang json.decoder 无法仅解码来自浏览器的请求

标签 json forms curl go

我的 golang 应用无法解码来自浏览器的表单,但在使用 curl 和 httpie 时成功。

给定这段代码:

    type Member struct {
    Username string `json:"username"`
    Email string `json:"email"`
    Password string `json:"password"`
    }



    func Register(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        var t Member
        json.NewDecoder(r.Body).Decode(&t)
        log.Println(t.Username)
        log.Println(t.Email)
        log.Println(t.Password)
        w.WriteHeader(204)
    }

打印!

curl -H "Content-Type: application/json"-X POST -d '{"username":"cesco","email":"cesco@gmail.com","password":"password"} ' http://localhost:5000/register

还有这个!

http -v -j  --form POST localhost:5000/register username="cesco" email="cesco@gmail.com" password="sadsa"

这不是

<form id="register" action="register"  method="post">
    <input type="text" name="username"><br>
    <input type="email" name="email"><br>
    <input type="password" name="password"><br>
    <input type="submit" value="Submit">
</form>

这都不是

var data = $('#register').serialize();
$.ajax({
    url: 'register', // php script to retern json encoded string
    data: data,  // serialized data to send on server
    beforeSend: function (xhr) {
        xhr.setRequestHeader("Content-type", "*/*");
    },
    dataType: 'json', // set recieving type - JSON in case of a question
    type: 'POST', // set sending HTTP Request type
    async: false,
    success: function (data) { // callback method for further manipulations

    },
    error: function (data) { // if error occured

    }
});

按照与问题相同的顺序登录

2016/02/09 13:56:12 cesco
2016/02/09 13:56:12 cesco@gmail.com
2016/02/09 13:56:12 password
2016/02/09 13:56:12 key: Accept value: [*/*]
2016/02/09 13:56:12 key: Content-Type value: [application/json]
2016/02/09 13:56:12 key: Content-Length value: [68]
2016/02/09 13:56:12 key: User-Agent value: [Mozilla/5.0 Gecko]
[13:56:12] 127.0.0.1 - - [09/Feb/2016:13:56:12 -0200] "POST /register HTTP/1.1" 204 0
2016/02/09 13:56:18 cesco
2016/02/09 13:56:18 cesco@gmail.com
2016/02/09 13:56:18 sadsa
2016/02/09 13:56:18 key: Accept-Encoding value: [gzip, deflate]
2016/02/09 13:56:18 key: Accept value: [application/json]
2016/02/09 13:56:18 key: User-Agent value: [HTTPie/0.8.0]
2016/02/09 13:56:18 key: Connection value: [keep-alive]
2016/02/09 13:56:18 key: Content-Type value: [application/json; charset=utf-8]
2016/02/09 13:56:18 key: Content-Length value: [70]
[13:56:18] 127.0.0.1 - - [09/Feb/2016:13:56:18 -0200] "POST /register HTTP/1.1" 204 0
[13:56:30] 127.0.0.1 - - [09/Feb/2016:13:56:30 -0200] "GET / HTTP/1.1" 200 747
2016/02/09 13:56:40 
2016/02/09 13:56:40 
2016/02/09 13:56:40 
2016/02/09 13:56:40 key: User-Agent value: [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36]
2016/02/09 13:56:40 key: Content-Type value: [application/x-www-form-urlencoded]
2016/02/09 13:56:40 key: Referer value: [http://localhost:5000/]
2016/02/09 13:56:40 key: Accept-Language value: [en-US,en;q=0.8,pt;q=0.6,es;q=0.4]
2016/02/09 13:56:40 key: Connection value: [keep-alive]
2016/02/09 13:56:40 key: Content-Length value: [53]
2016/02/09 13:56:40 key: Cache-Control value: [max-age=0]
2016/02/09 13:56:40 key: Accept value: [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8]
2016/02/09 13:56:40 key: Origin value: [http://localhost:5000]
2016/02/09 13:56:40 key: Upgrade-Insecure-Requests value: [1]
2016/02/09 13:56:40 key: Accept-Encoding value: [gzip, deflate]
[13:56:40] 127.0.0.1 - - [09/Feb/2016:13:56:40 -0200] "POST /register HTTP/1.1" 204 0
2016/02/09 13:56:59 
2016/02/09 13:56:59 
2016/02/09 13:56:59 
2016/02/09 13:56:59 key: Content-Type value: [*/*]
2016/02/09 13:56:59 key: Referer value: [http://localhost:5000/]
2016/02/09 13:56:59 key: Connection value: [keep-alive]
2016/02/09 13:56:59 key: Content-Length value: [53]
2016/02/09 13:56:59 key: X-Requested-With value: [XMLHttpRequest]
2016/02/09 13:56:59 key: User-Agent value: [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36]
2016/02/09 13:56:59 key: Accept value: [application/json, text/javascript, */*; q=0.01]
2016/02/09 13:56:59 key: Origin value: [http://localhost:5000]
2016/02/09 13:56:59 key: Accept-Encoding value: [gzip, deflate]
2016/02/09 13:56:59 key: Accept-Language value: [en-US,en;q=0.8,pt;q=0.6,es;q=0.4]
[13:56:59] 127.0.0.1 - - [09/Feb/2016:13:56:59 -0200] "POST /register HTTP/1.1" 204 0

最佳答案

您的 curl 示例使用 Content-Type: application/json 发布一个 json 编码的正文。浏览器不会以某种方式将 html 表单编码为 json。正如您在日志中看到的,您正在接收 Content-Type: application/x-www-form-urlencoded

如果您想从表单中获取值,请使用 Request.FormValue , 或 Request.PostFormValue

关于json - Golang json.decoder 无法仅解码来自浏览器的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35296534/

相关文章:

html - 在 CSS 下拉列表 (megamenu) 中使用表单选择在 IE7 中失败——解决方法?

php - 亚马逊 s3 存储桶上传图像 curl 异常 'data rewind wasn' t 可能 '

r - 发送数据时使用POST下载R中的文件

php - 我如何将 json 中的阿拉伯字符从 android 发送到 PHP

json - Firebase 数据库 : how to compare two values

java - 将嵌套 JSON 键转换为大写

file - 使用 cURL 检查文件是否存在于 FTPS 站点上

javascript - select2 ajax显示结果但无法选择

c# - 使用属性在表单之间传输数据

javascript - 为什么这个正则表达式不起作用?