From eb94afa6fb0200f18797990ffa08d8a0d01167a1 Mon Sep 17 00:00:00 2001 From: Casey Gowrie Date: Mon, 25 Apr 2022 06:08:46 -0400 Subject: [PATCH 1/3] Adding Content-Type headers to POST and PATCH requests --- client/deployment_create.go | 2 ++ client/environment_variable_upsert.go | 2 ++ client/file_create.go | 1 + client/project_create.go | 2 ++ client/project_domain_create.go | 2 ++ client/project_domain_update.go | 2 ++ client/project_update.go | 2 ++ client/team_create.go | 2 ++ 8 files changed, 15 insertions(+) diff --git a/client/deployment_create.go b/client/deployment_create.go index 5ac38fc5..47d81f63 100644 --- a/client/deployment_create.go +++ b/client/deployment_create.go @@ -154,6 +154,8 @@ func (c *Client) CreateDeployment(ctx context.Context, request CreateDeploymentR if err != nil { return r, err } + + req.Header.Set("Content-Type", "application/json") tflog.Trace(ctx, "creating deployment", map[string]interface{}{ "url": url, diff --git a/client/environment_variable_upsert.go b/client/environment_variable_upsert.go index 77a028f2..92354137 100644 --- a/client/environment_variable_upsert.go +++ b/client/environment_variable_upsert.go @@ -30,6 +30,8 @@ func (c *Client) UpsertEnvironmentVariable(ctx context.Context, projectID, teamI if err != nil { return err } + + req.Header.Set("Content-Type", "application/json") tflog.Trace(ctx, "upserting environment variable", map[string]interface{}{ "url": url, diff --git a/client/file_create.go b/client/file_create.go index e39e4d55..6f42af80 100644 --- a/client/file_create.go +++ b/client/file_create.go @@ -23,6 +23,7 @@ func (c *Client) CreateFile(ctx context.Context, filename, sha, content string) } req.Header.Add("x-vercel-digest", sha) + req.Header.Set("Content-Type", "application/octet-stream") tflog.Trace(ctx, "uploading file", map[string]interface{}{ "url": url, diff --git a/client/project_create.go b/client/project_create.go index ed86914f..15026317 100644 --- a/client/project_create.go +++ b/client/project_create.go @@ -57,6 +57,8 @@ func (c *Client) CreateProject(ctx context.Context, teamID string, request Creat return r, err } + req.Header.Set("Content-Type", "application/json") + tflog.Trace(ctx, "creating project", map[string]interface{}{ "url": url, "payload": string(mustMarshal(request)), diff --git a/client/project_domain_create.go b/client/project_domain_create.go index 148e42e4..23210daf 100644 --- a/client/project_domain_create.go +++ b/client/project_domain_create.go @@ -38,6 +38,8 @@ func (c *Client) CreateProjectDomain(ctx context.Context, projectID, teamID stri return r, err } + req.Header.Set("Content-Type", "application/json") + tflog.Trace(ctx, "creating project domain", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/project_domain_update.go b/client/project_domain_update.go index b0e634fd..044d0e75 100644 --- a/client/project_domain_update.go +++ b/client/project_domain_update.go @@ -34,6 +34,8 @@ func (c *Client) UpdateProjectDomain(ctx context.Context, projectID, domain, tea return r, err } + req.Header.Set("Content-Type", "application/json") + tflog.Trace(ctx, "updating project domain", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/project_update.go b/client/project_update.go index 6db6e523..cfb07068 100644 --- a/client/project_update.go +++ b/client/project_update.go @@ -43,6 +43,8 @@ func (c *Client) UpdateProject(ctx context.Context, projectID, teamID string, re return r, err } + req.Header.Set("Content-Type", "application/json") + tflog.Trace(ctx, "updating project", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/team_create.go b/client/team_create.go index 6b6be677..5e6fbf9e 100644 --- a/client/team_create.go +++ b/client/team_create.go @@ -35,6 +35,8 @@ func (c *Client) CreateTeam(ctx context.Context, request TeamCreateRequest) (r T return r, err } + req.Header.Set("Content-Type", "application/json") + tflog.Trace(ctx, "creating team", map[string]interface{}{ "url": url, "payload": payload, From c0617ddb706baa246b295e83f634885939ccea0f Mon Sep 17 00:00:00 2001 From: Casey Gowrie Date: Mon, 25 Apr 2022 11:20:56 -0400 Subject: [PATCH 2/3] gofmt --- client/deployment_create.go | 2 +- client/environment_variable_upsert.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/deployment_create.go b/client/deployment_create.go index 47d81f63..a552da89 100644 --- a/client/deployment_create.go +++ b/client/deployment_create.go @@ -154,7 +154,7 @@ func (c *Client) CreateDeployment(ctx context.Context, request CreateDeploymentR if err != nil { return r, err } - + req.Header.Set("Content-Type", "application/json") tflog.Trace(ctx, "creating deployment", map[string]interface{}{ diff --git a/client/environment_variable_upsert.go b/client/environment_variable_upsert.go index 92354137..ca77ed18 100644 --- a/client/environment_variable_upsert.go +++ b/client/environment_variable_upsert.go @@ -30,7 +30,7 @@ func (c *Client) UpsertEnvironmentVariable(ctx context.Context, projectID, teamI if err != nil { return err } - + req.Header.Set("Content-Type", "application/json") tflog.Trace(ctx, "upserting environment variable", map[string]interface{}{ From ceda2c14b788ee3809c76ded0ee881d129005e54 Mon Sep 17 00:00:00 2001 From: Casey Gowrie Date: Mon, 25 Apr 2022 13:33:37 -0400 Subject: [PATCH 3/3] Use application/json as default content-type for requests with a body, nil for body of GET, DELETE --- client/deployment_create.go | 2 -- client/deployment_get.go | 3 +-- client/environment_variable_upsert.go | 2 -- client/environment_variables_delete.go | 3 +-- client/environment_variables_get.go | 3 +-- client/project_create.go | 2 -- client/project_delete.go | 3 +-- client/project_domain_create.go | 2 -- client/project_domain_delete.go | 3 +-- client/project_domain_get.go | 3 +-- client/project_domain_update.go | 2 -- client/project_get.go | 3 +-- client/project_list.go | 3 +-- client/project_update.go | 2 -- client/request.go | 5 +++++ client/team_create.go | 2 -- client/team_delete.go | 3 +-- client/team_get.go | 3 +-- 18 files changed, 15 insertions(+), 34 deletions(-) diff --git a/client/deployment_create.go b/client/deployment_create.go index a552da89..5ac38fc5 100644 --- a/client/deployment_create.go +++ b/client/deployment_create.go @@ -155,8 +155,6 @@ func (c *Client) CreateDeployment(ctx context.Context, request CreateDeploymentR return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "creating deployment", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/deployment_get.go b/client/deployment_get.go index 1e8ab81f..eb63e79c 100644 --- a/client/deployment_get.go +++ b/client/deployment_get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -19,7 +18,7 @@ func (c *Client) GetDeployment(ctx context.Context, deploymentID, teamID string) ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return r, err diff --git a/client/environment_variable_upsert.go b/client/environment_variable_upsert.go index ca77ed18..77a028f2 100644 --- a/client/environment_variable_upsert.go +++ b/client/environment_variable_upsert.go @@ -31,8 +31,6 @@ func (c *Client) UpsertEnvironmentVariable(ctx context.Context, projectID, teamI return err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "upserting environment variable", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/environment_variables_delete.go b/client/environment_variables_delete.go index b67048ab..8966f6d2 100644 --- a/client/environment_variables_delete.go +++ b/client/environment_variables_delete.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -19,7 +18,7 @@ func (c *Client) DeleteEnvironmentVariable(ctx context.Context, projectID, teamI ctx, "DELETE", url, - strings.NewReader(""), + nil, ) if err != nil { return err diff --git a/client/environment_variables_get.go b/client/environment_variables_get.go index 5e48bfdb..c082b0ad 100644 --- a/client/environment_variables_get.go +++ b/client/environment_variables_get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -18,7 +17,7 @@ func (c *Client) getEnvironmentVariables(ctx context.Context, projectID, teamID ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return nil, err diff --git a/client/project_create.go b/client/project_create.go index 15026317..ed86914f 100644 --- a/client/project_create.go +++ b/client/project_create.go @@ -57,8 +57,6 @@ func (c *Client) CreateProject(ctx context.Context, teamID string, request Creat return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "creating project", map[string]interface{}{ "url": url, "payload": string(mustMarshal(request)), diff --git a/client/project_delete.go b/client/project_delete.go index cc475629..a1f3b94c 100644 --- a/client/project_delete.go +++ b/client/project_delete.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -20,7 +19,7 @@ func (c *Client) DeleteProject(ctx context.Context, projectID, teamID string) er ctx, "DELETE", url, - strings.NewReader(""), + nil, ) if err != nil { return err diff --git a/client/project_domain_create.go b/client/project_domain_create.go index 23210daf..148e42e4 100644 --- a/client/project_domain_create.go +++ b/client/project_domain_create.go @@ -38,8 +38,6 @@ func (c *Client) CreateProjectDomain(ctx context.Context, projectID, teamID stri return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "creating project domain", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/project_domain_delete.go b/client/project_domain_delete.go index 576aaa65..cf89ba03 100644 --- a/client/project_domain_delete.go +++ b/client/project_domain_delete.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -20,7 +19,7 @@ func (c *Client) DeleteProjectDomain(ctx context.Context, projectID, domain, tea ctx, "DELETE", url, - strings.NewReader(""), + nil, ) if err != nil { return err diff --git a/client/project_domain_get.go b/client/project_domain_get.go index 4793cd68..3cb2d863 100644 --- a/client/project_domain_get.go +++ b/client/project_domain_get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -30,7 +29,7 @@ func (c *Client) GetProjectDomain(ctx context.Context, projectID, domain, teamID ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return r, err diff --git a/client/project_domain_update.go b/client/project_domain_update.go index 044d0e75..b0e634fd 100644 --- a/client/project_domain_update.go +++ b/client/project_domain_update.go @@ -34,8 +34,6 @@ func (c *Client) UpdateProjectDomain(ctx context.Context, projectID, domain, tea return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "updating project domain", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/project_get.go b/client/project_get.go index 9b7bd1e5..26ff4b23 100644 --- a/client/project_get.go +++ b/client/project_get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -77,7 +76,7 @@ func (c *Client) GetProject(ctx context.Context, projectID, teamID string) (r Pr ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return r, err diff --git a/client/project_list.go b/client/project_list.go index 56ef7b54..fd7aab48 100644 --- a/client/project_list.go +++ b/client/project_list.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -20,7 +19,7 @@ func (c *Client) ListProjects(ctx context.Context, teamID string) (r []ProjectRe ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return r, err diff --git a/client/project_update.go b/client/project_update.go index cfb07068..6db6e523 100644 --- a/client/project_update.go +++ b/client/project_update.go @@ -43,8 +43,6 @@ func (c *Client) UpdateProject(ctx context.Context, projectID, teamID string, re return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "updating project", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/request.go b/client/request.go index e678b1b6..1a13f505 100644 --- a/client/request.go +++ b/client/request.go @@ -22,10 +22,15 @@ func (e APIError) Error() string { // doRequest is a helper function for consistently requesting data from vercel. // This manages: +// - Setting the default Content-Type for requests with a body // - Authorization via the Bearer token // - Converting error responses into an inspectable type // - Unmarshaling responses func (c *Client) doRequest(req *http.Request, v interface{}) error { + if req.Body != nil && req.Header.Get("Content-Type") == "" { + req.Header.Set("Content-Type", "application/json") + } + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.token)) resp, err := c.http().Do(req) if err != nil { diff --git a/client/team_create.go b/client/team_create.go index 5e6fbf9e..6b6be677 100644 --- a/client/team_create.go +++ b/client/team_create.go @@ -35,8 +35,6 @@ func (c *Client) CreateTeam(ctx context.Context, request TeamCreateRequest) (r T return r, err } - req.Header.Set("Content-Type", "application/json") - tflog.Trace(ctx, "creating team", map[string]interface{}{ "url": url, "payload": payload, diff --git a/client/team_delete.go b/client/team_delete.go index bcb01ec6..163a7cb0 100644 --- a/client/team_delete.go +++ b/client/team_delete.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -16,7 +15,7 @@ func (c *Client) DeleteTeam(ctx context.Context, teamID string) error { ctx, "DELETE", url, - strings.NewReader(""), + nil, ) if err != nil { return err diff --git a/client/team_get.go b/client/team_get.go index 82626cf9..1503f29d 100644 --- a/client/team_get.go +++ b/client/team_get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -21,7 +20,7 @@ func (c *Client) GetTeam(ctx context.Context, teamID, slug string) (r TeamRespon ctx, "GET", url, - strings.NewReader(""), + nil, ) if err != nil { return r, err