dongshuql24533 2014-10-28 15:16
浏览 8
已采纳

GoLang- PostForm请求,用于更新Cloudfoundry App的资源之一

I'm writing go code to access cloudfoundry platform and extract summary of data for all the apps that have been pushed on the cloud.

I can access every app individually and display the data related to each through an http.GET request but I want to be able to update/change some of the data specific to an app. This is sample data returned for one app named xzys:

{
   "metadata": {
      "guid": "71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "url": "/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "created_at": "2000-18-24T",
      "updated_at": "2000-18-27T"
   },
   "entity": {
      "name": "xzys",
      "production": false,
      "space_guid": "65050bcb-81c6-45a2-b6ef-5e7097c7ece1",
      "stack_guid": "89a4fb19-08ef-4c44-90f7-f222a5699fdc",
      "buildpack": null,
      "detected_buildpack": "null,
      "environment_json": {},
      "memory": 1024,
      "instances": 3,
      "disk_quota": 512,
      "state": "STARTED",
      "version": "67124c27-9958-45a7-afc5-5b27007348ab",
      "command": null,
      "console": false,
      "debug": null,
      "staging_task_id": "e901672f958b4ff39d4efb48290367e8",
      "package_state": "STAGED",
      "health_check_timeout": null,
      "staging_failed_reason": null,
      "docker_image": null,
      "package_updated_at": "2014-10-24T12:53:25+00:00",
      "space_url": "/v2/spaces/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "stack_url": "/v2/stacks/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
      "events_url": "/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e/events",
      "service_bindings_url": "/v2/apps/7-4b7625dq32908492b04f17e/service_bindings",
      "routes_url": "/v2/apps/771a3c77f-d2791232323-4b7625dq32908492b04f17e/routes"
   }
}

So I have a url (/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e) that contains the summary of info for an individual app. I've been using ahttp.Postform()` to this url to update just one of the resources of this app (I'm aiming to update the number of Instances for this app).

This is my code for updating the "Instances" resource of my app:

func UpdateInstances(token string){

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    client := &http.Client{Transport: tr}

    req, err := client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", url.Values{"instances" : 4})
    req.Header.Add("Authorization", token)
    if err != nil {
        log.Fatal(err)
    }
    defer req.Body.Close()
}

This doesn't give me any errors but also doesn't update the resource.

  • 写回答

2条回答 默认 最新

  • doujunchi1238 2014-10-30 16:37
    关注
    func UpdateInstances(token string) {
        tr := &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        }
       client := &http.Client{Transport: tr}
    
        //Displayed a form and used input value as "instances"
        instStr := r.FormValue("instances")
        inst, err := strconv.Atoi(instStr)//because the put request requires an integer
        if err != nil {
            panic(err)
        }
    
       type command struct {
            Instances int `json:"instances"`
        }
    
        bb, err := json.Marshal(&command{Instances: inst})
        if err != nil {
            log.Fatal(err)
        }
    
        url := "https://asjdklskjfkdsf.com"
    
        req, err := http.NewRequest("PUT", url, bytes.NewBuffer(bb))
        req.Header.Add("Authorization", token)
        if err != nil {
            log.Fatal(err)
        }
        defer req.Body.Close()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?