从JSON文件中读取数据,并将其作为POST请求发送。

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

Read data from a JSON file and send it as a Post request

问题

你好!以下是你提供的代码的翻译:

如何从 JSON 文件中读取数据并将其作为 POST 请求发送到 URI 终点。
我目前正在学习 Go 语言,并且正在进行我的第一个学习项目。

这是我的示例数据:

  1. // parent.json
  2. {"name":"Jade Copnell","age":16,"gender":"Agender","occupation":"Account Representative II","numbers":"178-862-5967","children":{"name":"Kayne Belsham","age":38,"gender":"Genderqueer","occupation":"Clinical Specialist","interest":"Re-engineered discrete methodology","number":"145-355-4123"},"friends":{"name":"Stephi Aries","age":74,"gender":"Genderqueer","occupation":"Senior Sales Associate","numbers":"873-726-1453","interests":"Self-enabling systematic function","methow":"24/7"}}

这是我编写的代码,当我运行下面的脚本时,我得到的输出数据类似于下面的内容,并且我还会将空数据发送到数据库中:

  1. "{\"name\":\"Jade Copnell\",\"age\":16,\"gender\":\"Agender\",\"occupation\":\"Account Representative II\",\"numbers\":\"178-862-5967\",\"children\":{\"name\":\"Kayne Belsham\",\"age\":38,\"gender\":\"Genderqueer\",\"occupation\":\"Clinical Specialist\",\"interest\":\"Re-engineered discrete methodology\",\"number\":\"145-355-4123\"},\"friends\":{\"name\":\"Stephi Aries\",\"age\":74,\"gender\":\"Genderqueer\",\"occupation\":\"Senior Sales Associate\",\"numbers\":\"873-726-1453\",\"interests\":\"Self-enabling systematic function\",\"methow\":\"24/7\"}}"
  1. func main() {
  2. // 打开文件。
  3. f, _ := os.Open("./go_data/parent.json")
  4. // 为文件创建一个新的 Scanner。
  5. scanner := bufio.NewScanner(f)
  6. // 循环遍历文件中的所有行并打印它们。
  7. for scanner.Scan() {
  8. responseBody := scanner.Text()
  9. postBody, _ := json.Marshal(responseBody)
  10. //fmt.Println(postBody)
  11. time.Sleep(2 * time.Second)
  12. webBody := bytes.NewBuffer(postBody)
  13. // fmt.Println(webBody)
  14. resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", webBody)
  15. if err != nil {
  16. log.Fatalf("An Error Occured %v", err)
  17. }
  18. time.Sleep(2 * time.Second)
  19. defer resp.Body.Close()
  20. }
  21. }

希望对你有帮助!如果你有任何其他问题,请随时问我。

英文:

How can i read data from a json file and send it as a post request to a uri endpoint.
I am currently learning the Go language and working on my first learning project.

This is my sample data

  1. // parent.json
  2. {"name":"Jade Copnell","age":16,"gender":"Agender","occupation":"Account Representative II","numbers":"178-862-5967","children":{"name":"Kayne Belsham","age":38,"gender":"Genderqueer","occupation":"Clinical Specialist","interest":"Re-engineered discrete methodology","number":"145-355-4123"},"friends":{"name":"Stephi Aries","age":74,"gender":"Genderqueer","occupation":"Senior Sales Associate","numbers":"873-726-1453","interests":"Self-enabling systematic function","methow":"24/7"}}

This is what I have written, when i run the below script, I tend to get a data similar to the below as output and I also get empty data sent to the database.

  1. "{\"name\":\"Jade Copnell\",\"age\":16,\"gender\":\"Agender\",\"occupation\":\"Account Representative II\",\"numbers\":\"178-862-5967\",\"children\":{\"name\":\"Kayne Belsham\",\"age\":38,\"gender\":\"Genderqueer\",\"occupation\":\"Clinical Specialist\",\"interest\":\"Re-engineered discrete methodology\",\"number\":\"145-355-4123\"},\"friends\":{\"name\":\"Stephi Aries\",\"age\":74,\"gender\":\"Genderqueer\",\"occupation\":\"Senior Sales Associate\",\"numbers\":\"873-726-1453\",\"interests\":\"Self-enabling systematic function\",\"methow\":\"24/7\"}}"
  1. func main() {
  2. // Open the file.
  3. f, _ := os.Open("./go_data/parent.json")
  4. // Create a new Scanner for the file.
  5. scanner := bufio.NewScanner(f)
  6. // Loop over all lines in the file and print them.
  7. for scanner.Scan() {
  8. responseBody := scanner.Text()
  9. postBody, _ := json.Marshal(responseBody)
  10. //fmt.Println(postBody)
  11. time.Sleep(2 * time.Second)
  12. webBody := bytes.NewBuffer(postBody)
  13. // fmt.Println(webBody)
  14. resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", webBody)
  15. if err != nil {
  16. log.Fatalf("An Error Occured %v", err)
  17. }
  18. time.Sleep(2 * time.Second)
  19. defer resp.Body.Close()
  20. }
  21. }

答案1

得分: 2

如果你这样做会怎样呢?http.Post的第三个参数是一个io.Reader接口,你的文件"f"实现了这个接口。

  1. package main
  2. import (
  3. "bufio"
  4. "log"
  5. "net/http"
  6. "os"
  7. "time"
  8. )
  9. func main() {
  10. // 打开文件。
  11. f, _ := os.Open("./go_data/parent.json")
  12. resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", f)
  13. if err != nil {
  14. log.Fatalf("发生错误 %v", err)
  15. }
  16. time.Sleep(2 * time.Second)
  17. defer resp.Body.Close()
  18. }
英文:

What if you do this instead. The third argument to http.Post is an io.Reader interface - that your file "f" implements.

  1. package main
  2. import (
  3. "bufio"
  4. "log"
  5. "net/http"
  6. "os"
  7. "time"
  8. )
  9. func main() {
  10. // Open the file.
  11. f, _ := os.Open("./go_data/parent.json")
  12. resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", f)
  13. if err != nil {
  14. log.Fatalf("An Error Occured %v", err)
  15. }
  16. time.Sleep(2 * time.Second)
  17. defer resp.Body.Close()
  18. }

huangapple
  • 本文由 发表于 2021年8月10日 05:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/68718582.html
匿名

发表评论

匿名网友

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

确定