Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
示例1,提交Json
请求
Code Block | ||
---|---|---|
| ||
g.Client().ContentJson().PostContent(ctx, "http://order.svc/v1/order", g.Map{ "uid" : 1, "sku_id" : 10000, "amount" : 19.99, "create_time" : "2020-03-26 12:00:00", }) |
通过调用ContentJson
链式操作方法,该请求将会将Content-Type
设置为application/json
,并且将提交参数自动编码为Json
:
Code Block | ||
---|---|---|
| ||
{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"} |
示例2,提交Xml
请求
Code Block | ||
---|---|---|
| ||
g.Client().ContentXml().PostContent(ctx, "http://order.svc/v1/order", g.Map{ "uid" : 1, "sku_id" : 10000, "amount" : 19.99, "create_time" : "2020-03-26 12:00:00", }) |
通过调用ContentXml
链式操作方法,该请求将会将Content-Type
设置为application/xml
,并且将提交参数自动编码为Xml
:
Code Block | ||
---|---|---|
| ||
<doc><amount>19.99</amount><create_time>2020-03-26 12:00:00</create_time><sku_id>10000</sku_id><uid>1</uid></doc> |
示例3,自定义ContentType
我们可以通过ContentType
方法自定义客户端请求的ContentType
。如果是给定的string/[]byte
参数,客户端将会直接将参数提交给服务端;如果是其他数据类型将会自动对参数执行url encode
再提交到服务端。
示例1:
Code Block | ||
---|---|---|
| ||
g.Client().ContentType("application/json").PostContent( ctx, "http://order.svc/v1/order", `{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}`, ) |
Code Block | ||
---|---|---|
| ||
g.Client().ContentType("application/json; charset=utf-8").PostContent( ctx, "http://order.svc/v1/order", `{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}`, ) |
提交的参数如下:
Code Block | ||
---|---|---|
| ||
{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"} |
示例2:
Code Block | ||
---|---|---|
| ||
g.Client().ContentType("text/htmlapplication/x-www-form-urlencoded; charset=utf-8").GetContent( ctx, "http://order.svc/v1/order", g.Map{ "category" : 1, "sku_id" : 10000, }, ) |
提交的参数如下:
Code Block | ||
---|---|---|
| ||
category=1&sku_id=10000 |
Panel | ||
---|---|---|
| ||
|