Read a post parameter sent through axios in golang

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

Read a post parameter sent through axios in golang

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go语言的新手,正在尝试理解这段代码。我遇到了以下问题。

我正在使用axios发送POST请求,代码如下:

const options = {
  data: {
    test: this.state.value,
  },
  method: 'POST',
  url: `/test`,
};
console.log(options)
axios.request(options).then(
  () => {
    console.log(this.state.value);
  },
  (error) => {
    console.log(error);
  },
);

现在在Go部分,我正在尝试读取它。但是我不知道如何在Go中读取它。我尝试了以下代码,但它不起作用。它只打印出test Value。有人可以帮帮我吗?谢谢。

func routetest(w http.ResponseWriter, r *http.Request) {
	test, _ := param.String(r, "test")
	fmt.Printf("test Value")
	fmt.Printf(test)
	fmt.Printf("test Value")
}

更新后的代码

func routeTest(w http.ResponseWriter, r *http.Request) {
	type requestBody struct {
		test string `json:"test"`
	}
	body := requestBody{}
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&body); err != nil {
		// 错误处理
		return
	}
	defer r.Body.Close()
	test := body.test
	fmt.Printf(test)
}
英文:

I am new to golang and try to understand this. I facing the following problem.

I am sending axios post as below

const options = {
  data: {
    test: this.state.value,
  },
  method: 'POST',
  url: `/test`,
};
console.log(options)
axios.request(options).then(
  () => {
    console.log(this.state.value);
  },
  (error) => {
    console.log(error);
  },
);

And now in the Go part, I am trying to read it. But I am not getting how to read it in Go. I am tried with the following code, but it is not working. It is printing only test Value. Can anyone help me? Thanks.

func routetest(w http.ResponseWriter, r *http.Request) {
	test, _ := param.String(r, "test")
	fmt.Printf("test Value")
	fmt.Printf(test)
	fmt.Printf("test Value")
}

Updated Code

func routeTest(w http.ResponseWriter, r *http.Request) {
	type requestBody struct {
		test string `json:"test"`
	}
	body := requestBody{}
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&body); err != nil {
		// some error handling
		return
	}
	defer r.Body.Close()
	test := body.test
	fmt.Printf(test)
}

答案1

得分: 1

最简单的方法是将你的身体解码为类似JSON的结构,然后从中获取你的值。

import (
    "encoding/json"
    "fmt"
    "net/http"
)

type requestBody struct {
    Test       string `json:"test"`
}

func routeTest(w http.ResponseWriter, r *http.Request) {
    body := requestBody{}
    decoder := json.NewDecoder(r.Body)
    if err := decoder.Decode(&body); err != nil {
        // 错误处理
        return
    }
    defer r.Body.Close()
    test := body.Test
    fmt.Printf(test)
}
英文:

The easiest way is to decode your body to json-like struct and then just get your value from there

import (
    "encoding/json"
    "fmt"
    "net/http"
)

type requestBody struct {
    Test       string `json:"test"`
}

func routeTest(w http.ResponseWriter, r *http.Request) {
    body := requestBody{}
    decoder := json.NewDecoder(r.Body)
    if err := decoder.Decode(&body); err != nil {
        // some error handling
        return
    }
    defer r.Body.Close()
    test := body.Test
    fmt.Printf(test)
}

huangapple
  • 本文由 发表于 2021年6月4日 14:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/67832735.html
匿名

发表评论

匿名网友

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

确定