douhuo3696 2018-02-27 00:09
浏览 71

使用golang的http请求中的nil chan,中断通话

I am attempting to call the name.com API (and am successful, except for one call... When I attempt to call their Search() method, I am getting an error.

Here is my code:

func TestExecute() string{
    client := &http.Client{}
    body := []byte("keyword=web")
    req, newReqErr := http.NewRequest("POST", "https://api.name.com/v4/domains:search", bytes.NewBuffer(body))
    check("new Request Error: ", newReqErr)

    req.SetBasicAuth("username", "[TOKEN ]")

    fmt.Println(req)

    resp, doErr := client.Do(req)
    check("client Do Error: ", doErr)

    bodyText, readErr := ioutil.ReadAll(resp.Body)
    check("Read error: ", readErr)

    fmt.Printf("Result: %s
", bodyText)

    return fmt.Sprintf("StatusCode: %d
%s", resp.StatusCode, bodyText)

}

Here is the request text:

&{POST https://api.name.com/v4/domains:search HTTP/1.1 1 1 map[Authorization:[Basic cldokleodjjAzMWNlODY2MjRlYmI5xxkxjdkfOWI4ZjNhMWQ5NmVmOGIA5YTA=]] {keyword=web} 11 [] false api.name.com map[] map[] <nil> map[]   <nil> <nil>}

and here is the response:

{"message":"Invalid Argument","details":"Error occurred during parsing: Cannot decode json string."}

It appears that some empty structs (internals, not mine) are being introduced into the header. OR name.com is parsing incorrectly.

Thoughts?

  • 写回答

1条回答 默认 最新

  • doutuo7609 2018-02-27 06:39
    关注

    According to Name.com API Documentation the body of a POST /v4/domains:search request needs to be represented as JSON. They show an example JSON body in the sidebar.

    {"keyword":"example"}
    

    To produce the JSON body and be sure that all escaping has happened correctly, let's define the following JSON formattable struct.

    type DomainSearch struct {
        Keyword string `json:"keyword"`
    }
    

    Then replace your body := []byte("keyword=web") with a JSON body:

    body, err := json.Marshal(DomainSearch{"web"})
    

    Assuming you have a valid Name.com API token you should see a 200 response come back with a JSON response body, something like:

    {
      "results": [
        {
          "domainName": "web.blog",
          "sld": "web",
          "tld": "blog"
        },
        {
          "domainName": "web.cam",
          "sld": "web",
          "tld": "cam"
        },
        {
          "domainName": "web.camera",
          "sld": "web",
          "tld": "camera",
          "purchasable": true,
          "premium": true,
          "purchasePrice": 500,
          "purchaseType": "registration",
          "renewalPrice": 500
        },
        {
          "domainName": "web.cloud",
          "sld": "web",
          "tld": "cloud"
        },
        ...
      ]
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分