GoLang- 使用PostForm请求来更新Cloudfoundry应用程序的资源之一

huangapple go评论94阅读模式
英文:

GoLang- PostForm request for updating one of the resources of Cloudfoundry App

问题

我正在编写Go代码来访问Cloud Foundry平台,并提取在云上推送的所有应用程序的摘要数据。

我可以通过http.GET请求访问每个应用程序并显示与每个应用程序相关的数据,但我希望能够更新/更改特定于应用程序的某些数据。这是一个名为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"
}
}

所以我有一个包含单个应用程序信息摘要的URL(/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e)。我一直在使用http.PostForm()向此URL发送请求,以更新此应用程序的某个资源(我打算更新此应用程序的实例数)。

这是我用于更新应用程序的“Instances”资源的代码:

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()
}

这不会给我任何错误,但也不会更新资源。

英文:

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 a http.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.

答案1

得分: 0

http://golang.org/pkg/net/url/#Values
> type Values map[string][]string

你可以看到Values是一个map,其键的类型为string,值的类型为[]string

req, err := client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", url.Values{"instances" : 4})

所以你已经做得很好,直到值的部分,当你尝试传入一个数字时,它期望的是一个[]string。将其改为:

client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
                  url.Values{ "instances": { "4" }})

或者你可以这样做:

v := url.Values{}
v.Set("instances", "4")
client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", v)

这样应该可以工作。

英文:

http://golang.org/pkg/net/url/#Values
> type Values map[string][]string

You see that Values is a map with keys that are of type string and values that are []string.

req, err := client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", url.Values{"instances" : 4})

So you have it all fine, right up until the value, when you try and pass in a number, where it expects an []string. Change it to

client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e",
                  url.Values{ "instances": { "4" }})

Or you could do

v := url.Values{}
v.Set("instances", "4")
client.PostForm("/v2/apps/71a3c77f-d2791232323-4b7625dq32908492b04f17e", v)

And it should work

答案2

得分: -3

func UpdateInstances(token string) {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}

    // 显示一个表单并使用输入值作为 "instances"
    instStr := r.FormValue("instances")
    inst, err := strconv.Atoi(instStr) // 因为 put 请求需要一个整数
    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()
}
英文:
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()
}

huangapple
  • 本文由 发表于 2014年10月28日 23:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/26611918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定