Example 1, Submit Json Request

  1. g.Client().ContentJson().PostContent(ctx, "http://order.svc/v1/order", g.Map{
  2. "uid" : 1,
  3. "sku_id" : 10000,
  4. "amount" : 19.99,
  5. "create_time" : "2020-03-26 12:00:00",
  6. })

By calling the ContentJson chain operation method, this request will set the Content-Type to application/json and automatically encode the submission parameters to Json:

  1. {"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}

Example 2, Submit Xml Request

  1. g.Client().ContentXml().PostContent(ctx, "http://order.svc/v1/order", g.Map{
  2. "uid" : 1,
  3. "sku_id" : 10000,
  4. "amount" : 19.99,
  5. "create_time" : "2020-03-26 12:00:00",
  6. })

By calling the ContentXml chain operation method, this request will set the Content-Type to application/xml and automatically encode the submission parameters to Xml:

  1. <doc><amount>19.99</amount><create_time>2020-03-26 12:00:00</create_time><sku_id>10000</sku_id><uid>1</uid></doc>

Example 3, Custom ContentType

We can customize the client’s request ContentType through the ContentType method. If the given parameter is string/[]byte, the client will directly submit the parameter to the server; if it is another data type, the client will automatically perform url encode on the parameter before submitting it to the server.

Example 1:

  1. g.Client().ContentType("application/json").PostContent(
  2. ctx,
  3. "http://order.svc/v1/order",
  4. `{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}`,
  5. )

or

  1. g.Client().ContentType("application/json; charset=utf-8").PostContent(
  2. ctx,
  3. "http://order.svc/v1/order",
  4. `{"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}`,
  5. )

Submitted parameters are as follows:

  1. {"uid":1,"sku_id":10000,"amount":19.99,"create_time":"2020-03-26 12:00:00"}

Example 2:

  1. g.Client().ContentType("application/x-www-form-urlencoded; charset=utf-8").GetContent(
  2. ctx,
  3. "http://order.svc/v1/order",
  4. g.Map{
  5. "category" : 1,
  6. "sku_id" : 10000,
  7. },
  8. )

Submitted parameters are as follows:

  1. category=1&sku_id=10000