Golang http.NewRequest:内容类型无效

时间:2015-12-09 01:11:31

标签: struct go

我正在尝试使用Go struct发布http.POST,但我没有成功。

请看:

//SKUStock definition
type SkuStock struct {
    Sku string `json:"id"`;
    Quantity int64 `json:"stockQuantity"`;
}

type StockJson struct {
    Items []SkuStock `json:"skus"`;
}

从数据库加载一些数据后,我有以下内容,以便自己执行POST:

//Sends the skuStock Information to server
func SendSkuData(stockItems []SkuStock) {

//creates a json like {"skus":[{"id":"ITEM1576","stockQuantity":995}]}
    stockPayload, err := json.Marshal(StockJson{stockItems}); 
    if err != nil {
        log.Fatal(err);
    }

    client := &http.Client{}

    apiRequestUrl := os.Getenv("api.protocol") + "://" + os.Getenv("api.address") + "/batch/sku/stock";
    req, err := http.NewRequest("POST", apiRequestUrl, bytes.NewBuffer(stockPayload));
    req.Header.Add("Content-Type", "application/json;charset=utf-8");
    req.SetBasicAuth(os.Getenv("api.key"), "");

    log.Printf("Request body: %s", string(stockPayload));
    log.Printf("Request %s", req);

    resp, err := client.Do(req);
    if(err != nil) {
        log.Fatal(err);
    }
    defer resp.Body.Close();

    log.Printf("Response %s", resp);

}

不幸的是,我从服务器获得的回报并不像预期的那样:

2015/12/08 23:42:47 Request body: {"skus":[{"id":"ITEM1576","stockQuantity":995}]}
2015/12/08 23:42:47 Request &{POST http://api-sandbox.bonmarketplace.com.br/batch/sku/stock HTTP/1.1 %!s(int=1) %!s(    int=1) map[Authorization:[Basic REMzRjAyNjUwNDE5RDAzQUJBNjEyM0E0QUZCMzJGQUQ6] Content-Type:[application/    json;charset=utf-8]] {{"skus":[{"id":"ITEM1576","stockQuantity":995}]}} %!s(int64=48) [] %!s(bool=false) api-sandbox.    bonmarketplace.com.br map[] map[] %!s(*multipart.Form=<nil>) map[]   %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct     {}=<nil>)}
2015/12/08 23:42:47 Response &{415 Unsupported Media Type %!s(int=415) HTTP/1.1 %!s(int=1) %!s(int=1) map[Server:[    nginx/1.7.5] Date:[Wed, 09 Dec 2015 01:40:18 GMT] Content-Type:[application/json;charset=utf-8] Content-Length:[163]     Connection:[keep-alive] X-Access-Control-Realm:[external]] %!s(*http.bodyEOFSignal=&{0xc820122000 {0 0} false <nil>     0xd78a0 0xd7840}) %!s(int64=163) [] %!s(bool=false) map[] %!s(*http.Request=&{POST 0xc820064480 HTTP/1.1 1 1 map[Content-    Type:[application/json;charset=utf-8] Authorization:[Basic REMzRjAyNjUwNDE5RDAzQUJBNjEyM0E0QUZCMzJGQUQ6]] {0xc82000e460}     48 [] false api-sandbox.bonmarketplace.com.br map[] map[] <nil> map[]   <nil> <nil>}) %!s(*tls.ConnectionState=<nil>)}

编辑:插入帖子正文(程序&#39;输出的第一行)。如果我在Postman上运行请求,使用此主体,它按预期工作。服务器不会检查Accept标头,只检查已设置为Content-Type的{​​{1}}。

我做错了什么?我花了最后几个小时研究这个,但没有运气。

1 个答案:

答案 0 :(得分:0)

打印响应正文后,返回以下内容:

2015/12/09 00:32:11 Response Body {"httpStatusCode":415,"errorCode":"415","message":"Content-type:[application/json;charset=utf-8] invalido. Utilizar content-type:[application/json;charset=UTF-8]"}

来自葡萄牙语:“内容类型无效。请使用application/json;charset=UTF-8

详细信息是在UTF-8字符串上使用的情况,该字符串应该是大写的,而不是像以前那样低。感谢@evanmcdonnal和@CodingPickle的支持。