doushuhuai7247 2015-12-09 01:11
浏览 177
已采纳

Golang http.NewRequest:无效的内容类型

I am trying to post a struct using Go's http.POST, however I am not being succeeded with that.

Please look:

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

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

After loading some data from a database, I have the following, in order to perform the POST itself:

//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);

}

Unfortunately, the return I get from server is not as expected:

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>)}

Edit: inserted the post body(first line on program's output). If I run the request on Postman, with this body, it works as expected. Server does not check Accept header, just the Content-Type, which is already set to application/json;charset=utf-8.

What am I doing wrong? I have spent the last hours looking into this, but no luck at all.

  • 写回答

1条回答 默认 最新

  • doupi6737 2015-12-09 02:35
    关注

    After printing the response body, the following has been returned:

    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]"}
    

    From portuguese: "Invalid Content-Type. Please use application/json;charset=UTF-8.

    The detail was in the case used on UTF-8 string, which should be uppercase, not lower as previously. Thanks @evanmcdonnal and @CodingPickle for support.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号