1. 转换带 JSON 载荷的 POST 请求
后端开发工程师背景
需要在 Go 服务中调用一个第三方的用户注册接口,该接口接收 JSON 格式的请求体。
问题
手动编写 Go 的 http.NewRequest 并设置 JSON 字符串和 Header 比较繁琐且容易出错。
如何使用
将包含 -X POST、-H "Content-Type: application/json" 和 -d 参数的 cURL 命令粘贴到输入框中,保持“格式化请求体”勾选。
curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -d '{"username":"testuser","email":"[email protected]"}'结果
获得一段完整的 Go 代码,其中包含 strings.NewReader 承载的 JSON 字符串,以及正确设置的 Content-Type 请求头和 http.DefaultClient.Do 调用。