Go에서 POST 요청으로 JSON 문자열을 전송하려면 어떻게 해야 합니까?
Apiary와 함께 작업해 본 결과 JSON을 모킹 서버로 전송하기 위한 범용 템플릿을 만들고 다음 코드를 가지고 있습니다.
package main
import (
"encoding/json"
"fmt"
"github.com/jmcvetta/napping"
"log"
"net/http"
)
func main() {
url := "http://restapi3.apiary.io/notes"
fmt.Println("URL:>", url)
s := napping.Session{}
h := &http.Header{}
h.Set("X-Custom-Header", "myvalue")
s.Header = h
var jsonStr = []byte(`
{
"title": "Buy cheese and bread for breakfast."
}`)
var data map[string]json.RawMessage
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
}
resp, err := s.Post(url, &data, nil, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("response Status:", resp.Status())
fmt.Println("response Headers:", resp.HttpResponse().Header)
fmt.Println("response Body:", resp.RawText())
}
이 코드는 JSON을 올바르게 송신하지 않지만, 그 이유는 알 수 없습니다.JSON 문자열은 콜마다 다를 수 있습니다.사용할 수 없습니다Struct이걸 위해서.
낮잠은 잘 모르는데 골랑의 잠은net/http패키지는 정상적으로 동작합니다(Playground).
func main() {
url := "http://restapi3.apiary.io/notes"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
그냥 사용하시면 됩니다.postjson을 올립니다.
values := map[string]string{"username": username, "password": password}
jsonValue, _ := json.Marshal(values)
resp, err := http.Post(authAuthenticatorUrl, "application/json", bytes.NewBuffer(jsonValue))
이미 구조를 가지고 있다면.
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
// .....
type Student struct {
Name string `json:"name"`
Address string `json:"address"`
}
// .....
body := &Student{
Name: "abc",
Address: "xyz",
}
payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(body)
req, _ := http.NewRequest("POST", url, payloadBuf)
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
return e
}
defer res.Body.Close()
fmt.Println("response Status:", res.Status)
// Print the body to the stdout
io.Copy(os.Stdout, res.Body)
전체 요지.
표준 net/http 패키지와 더불어 net/http를 감싸는 GoRequest를 사용하면 json이나 structure에 대해 너무 생각하지 않고 편하게 생활할 수 있습니다.단, 둘 다 하나의 요청으로 믹스 매칭할 수도 있습니다! (자세한 내용은 gorequest github 페이지에서 확인하실 수 있습니다.)
따라서 최종적으로 코드는 다음과 같습니다.
func main() {
url := "http://restapi3.apiary.io/notes"
fmt.Println("URL:>", url)
request := gorequest.New()
titleList := []string{"title1", "title2", "title3"}
for _, title := range titleList {
resp, body, errs := request.Post(url).
Set("X-Custom-Header", "myvalue").
Send(`{"title":"` + title + `"}`).
End()
if errs != nil {
fmt.Println(errs)
os.Exit(1)
}
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
fmt.Println("response Body:", body)
}
}
이것은 어떻게 달성하고 싶은지에 따라 달라집니다.저는 당신과 같은 문제가 있어서 이 라이브러리를 만들었습니다.그리고 저는 제 코드베이스와 프로덕션 시스템에서 더 짧고, 사용하기 쉽고, 유지보수가 더 쉬운 코드를 원합니다.
http 또는 https에 대한 투고 요청 예시
//Encode the data
postBody, _ := json.Marshal(map[string]string{
"name": "Test",
"email": "Test@Test.com",
})
responseBody := bytes.NewBuffer(postBody)
//Leverage Go's HTTP Post function to make request
resp, err := http.Post("https://postman-echo.com/post", "application/json", responseBody)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
log.Printf(sb)
다른 답변에서 설명한 것처럼 큰 요청 본문에 io.Pipe를 사용합니다.이 방법에서는 데이터를 JSON 인코더에서 네트워크로 스트리밍함으로써 요구 본문 전체가 메모리에 구축되는 것을 방지합니다.
이 답변은 오류 처리 방법을 보여줌으로써 다른 답변에 기초합니다.항상 오류를 처리합니다!
- 파이프의 CloseWithError 함수를 사용하여 인코딩 오류를 http에서 반환된 오류로 전파합니다.포스트.
- http에서 반환된 오류를 처리합니다.포스트.
- 응답 본문을 닫습니다.
코드는 다음과 같습니다.
r, w := io.Pipe()
go func() {
w.CloseWithError(json.NewEncoder(w).Encode(data))
}()
// Ensure that read side of pipe is closed. This
// unblocks goroutine in scenario where http.Post
// errors out before reading the entire request body.
defer r.Close()
resp, err := http.Post(url, r)
if err != nil {
// Adjust error handling here to meet application requrirements.
log.Fatal(err)
}
defer resp.Body.Close()
// Use the response here.
전송할 데이터가 많은 경우 파이프를 사용할 수 있습니다.
package main
import (
"encoding/json"
"io"
"net/http"
)
func main() {
m := map[string]int{"SNG_ID": 75498415}
r, w := io.Pipe()
go func() {
json.NewEncoder(w).Encode(m)
w.Close()
}()
http.Post("https://stackoverflow.com", "application/json", r)
}
https://golang.org/pkg/io#Pipe
그렇게 하려면 이 맵을 사용하여 json 문자열의 마샬링을 해제해야 합니다.
var data map[string]interface{}
그러나 매번 json을 변경하고 요구 본문의 초기화를 편리하게 하기 위해 이 맵을 사용하여 json 본문을 작성할 수 있습니다.
var bodyJsonMap map[string]interface{}{
"key1": val1,
"key2": val2,
...
}
그럼 줄에 맞춰 정렬해
언급URL : https://stackoverflow.com/questions/24455147/how-do-i-send-a-json-string-in-a-post-request-in-go
'programing' 카테고리의 다른 글
| woocommerce get_woocommerce_displays() (0) | 2023.02.22 |
|---|---|
| MongoDB용 RAM에 "작업 세트"를 장착하는 것은 무엇을 의미합니까? (0) | 2023.02.22 |
| Woocommerce는 카트 내에 1개의 제품만 허용해야 합니다.제품이 이미 장바구니에 있고 다른 1개가 추가되면 이전 1개를 제거해야 합니다. (0) | 2023.02.22 |
| Angular 어플리케이션에서 메모리 누수를 포착하는 방법 (0) | 2023.02.22 |
| AWS Lambda에서 DynamoDB 데이터를 일반 JSON으로 포맷 (0) | 2023.02.22 |