Skip to content

Commit f2cd789

Browse files
authored
Merge pull request #12 from mario-imperato/main
Fix in the auth header function and in the json request function
2 parents 9dcb828 + cbc8039 commit f2cd789

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

restclient.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ func (c *RestClient) buildJsonRequest(method, url string, params interface{}) *h
124124

125125
func (c *RestClient) addAuthHeader(req *http.Request, method, resType, resId string) *http.Request {
126126
now := time.Now().In(locGmt)
127-
stringToSign := strings.ToLower(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", method, resType, resId, now.Format(time.RFC1123), ""))
127+
/*
128+
* M.A.I. 2022-02-16
129+
* The original statement had a single ToLower. In the resulting string the resId gets lowered when from MS Docs it should be left unaltered
130+
* I came across an error on a collection with a mixed case name...
131+
* stringToSign := strings.ToLower(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", method, resType, resId, now.Format(time.RFC1123), ""))
132+
*/
133+
stringToSign := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", strings.ToLower(method), strings.ToLower(resType), resId, strings.ToLower(now.Format(time.RFC1123)), "")
128134
h := hmac.New(sha256.New, c.authKey)
129135
h.Write([]byte(stringToSign))
130136
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
@@ -543,7 +549,19 @@ func (c *RestClient) QueryDocuments(query QueryReq) *RespQueryDocs {
543549

544550
method := "POST"
545551
url := c.endpoint + "/dbs/" + query.DbName + "/colls/" + query.CollName + "/docs"
546-
req := c.buildJsonRequest(method, url, map[string]interface{}{"query": query.Query, "parameters": query.Params})
552+
553+
/*
554+
* M.A.I. 2022-02-16
555+
* In case of requests with no parameters the original form created a request with parameters set to nil. Apparently MS complaints about it.
556+
* Original form
557+
* req := c.buildJsonRequest(method, url, map[string]interface{}{"query": query.Query, "parameters": query.Params})
558+
*/
559+
requestBody := make(map[string]interface{}, 0)
560+
requestBody["query"] = query.Query
561+
if query.Params != nil {
562+
requestBody["parameters"] = query.Params
563+
}
564+
req := c.buildJsonRequest(method, url, requestBody)
547565
req = c.addAuthHeader(req, method, "docs", "dbs/"+query.DbName+"/colls/"+query.CollName)
548566
req.Header.Set("Content-Type", "application/query+json")
549567
req.Header.Set("X-Ms-Documentdb-Isquery", "true")

0 commit comments

Comments
 (0)