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

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

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

问题

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

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

这是我的示例数据:

// parent.json

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

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

"{\"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\"}}"
func main() {
	// 打开文件。
	f, _ := os.Open("./go_data/parent.json")
	// 为文件创建一个新的 Scanner。
	scanner := bufio.NewScanner(f)
	// 循环遍历文件中的所有行并打印它们。
	for scanner.Scan() {
		responseBody := scanner.Text()

		postBody, _ := json.Marshal(responseBody)
		//fmt.Println(postBody)
		time.Sleep(2 * time.Second)
		webBody := bytes.NewBuffer(postBody)
		// fmt.Println(webBody)

		resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", webBody)

		if err != nil {
			log.Fatalf("An Error Occured %v", err)
		}
		time.Sleep(2 * time.Second)
		defer resp.Body.Close()
	}
}

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

英文:

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

// parent.json

{"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.

"{\"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\"}}"
func main() {
	// Open the file.
	f, _ := os.Open("./go_data/parent.json")
	// Create a new Scanner for the file.
	scanner := bufio.NewScanner(f)
	// Loop over all lines in the file and print them.
	for scanner.Scan() {
		responseBody := scanner.Text()

		postBody, _ := json.Marshal(responseBody)
		//fmt.Println(postBody)
		time.Sleep(2 * time.Second)
		webBody := bytes.NewBuffer(postBody)
		// fmt.Println(webBody)

		resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", webBody)

		if err != nil {
			log.Fatalf("An Error Occured %v", err)
		}
		time.Sleep(2 * time.Second)
		defer resp.Body.Close()
	}
}

答案1

得分: 2

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

package main

import (
	"bufio"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	// 打开文件。
	f, _ := os.Open("./go_data/parent.json")

	resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", f)

	if err != nil {
		log.Fatalf("发生错误 %v", err)
	}
	time.Sleep(2 * time.Second)
	defer resp.Body.Close()
}
英文:

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

package main

import (
	"bufio"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	// Open the file.
	f, _ := os.Open("./go_data/parent.json")

	resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", f)

	if err != nil {
		log.Fatalf("An Error Occured %v", err)
	}
	time.Sleep(2 * time.Second)
	defer resp.Body.Close()
}

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:

确定