# PUT请求发送JSON对象

`http.Client`并没有提供PUT请求的快捷方式，所以我们构造一个`http.Request`对象并使用`http.Client.Do(req *http.Request)`来完成请求。

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

type User struct {
	Name  string
	Email string
}

func main() {
	user := User{
		Name:  "John Doe",
		Email: "johndoe@example.com",
	}

	d, err := json.Marshal(user)
	if err != nil {
		log.Fatalf("json.Marshal() failed with '%s'\n", err)
	}

	client := &http.Client{}
	client.Timeout = time.Second * 15

	uri := "https://httpbin.org/put"
	body := bytes.NewBuffer(d)
	req, err := http.NewRequest(http.MethodPut, uri, body)
	if err != nil {
		log.Fatalf("http.NewRequest() failed with '%s'\n", err)
	}

	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalf("client.Do() failed with '%s'\n", err)
	}

	defer resp.Body.Close()
	d, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)
	}

	fmt.Printf("Response status code: %d, text:\n%s\n", resp.StatusCode, string(d))
}

```

{% endtab %}

{% tab title="Output" %}

```http
Response status code: 200, text:
{
  "args": {}, 
  "data": "{\"Name\":\"John Doe\",\"Email\":\"johndoe@example.com\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "49", 
    "Content-Type": "application/json; charset=utf-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/2.0", 
    "X-Amzn-Trace-Id": "Root=1-5ebf5b94-36212820e3fd84b4a60224cc"
  }, 
  "json": {
    "Email": "johndoe@example.com", 
    "Name": "John Doe"
  }, 
  "origin": "199.168.140.214", 
  "url": "https://httpbin.org/put"
}

```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://denglj.gitbook.io/essential-go/common-standard-libraries/36-http-client/put-request-of-json-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
