doufu6130 2017-03-07 17:29
浏览 44

进行Golang发布时禁止使用403

I have a REST endpoint provided by a Java Spring Boot app that I am trying to hit with a simple Go (go1.8 darwin/amd64) program. I have tried to hit it in 2 separate ways:

const (
    LOCAL_BASE         = "http://localhost:11400"
    ADD_FUNDS_ENDPOINT = "/sub-accounts/%d/add-funds"
)

type InputArgumentsJson struct {
    Amount           float64 `json:"amount"`
    BufferAmount     float64 `json:"bufferAmount"`
    FromSubAccountID int     `json:"fromSubAccountId"`
}

...

// set up json body
inputArguments := &InputArgumentsJson{Amount: 1100, BufferAmount: 0,
    FromSubAccountID: spendFundId}
bytes := new(bytes.Buffer)
json.NewEncoder(bytes).Encode(inputArguments)

// set up the url to call
endpoint := fmt.Sprintf(ADD_FUNDS_ENDPOINT, billSetAsideId)
url := LOCAL_BASE + endpoint

client := &http.Client{}

req, err := http.NewRequest("POST", url, bytes)
if err != nil {
    fmt.Println("ERROR WHILE BUILDING REQUEST")
    fmt.Println(err)
}

req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
    fmt.Println("ERROR WHILE MAKING REQUEST")
    fmt.Println(err)
}
fmt.Println(resp)

and:

resp, err := http.Post(url, "application/json; charset=utf-8", bytes)
if err != nil {
    fmt.Println("ERROR WHILE MAKING REQUEST")
    fmt.Println(err)
}
fmt.Println(resp)

I added a print out to see if the URL and JSON body were right and they appeared to be:

fmt.Printf("
Making request to %s with:
 %s
", url, bytes)

printed out

Making request to http://localhost:11400/sub-accounts/167/add-funds with:
{"amount":1100,"bufferAmount":0,"fromSubAccountId":166}

but I am getting a 403 Forbidden when running it.

I can hit this endpoint without fail using both Postman and the following Curl command:

curl -H "Content-Type: application/json" -X POST -d '{"amount":1100,"bufferAmount":0,"fromSubAccountId":166}' http://localhost:11400/sub-accounts/167/add-funds

Does anyone have any thoughts on what I may be missing here? To add further to the intrigue I can successfully hit a different GET endpoint from the Go program.

Thanks in advance!

Edited to show that I was catching the errors and the surrounding logic for creating the URL and JSON body

  • 写回答

4条回答 默认 最新

  • doutou6803 2017-03-07 18:24
    关注

    You did not show what bytes is but that's most probably the problem: both NewRequest and Post take an io.Reader and read the body from that reader. This reading typically can happen only once (think of a stream). When you fmt.Printf("%s") the io.Reader this printing will consume the content (most probably by invoking a String() method) and the actual request is done with an empty body.

    Always show whole code. And do not try to read twice from an io.Reader.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c