Skip to content

Commit

Permalink
Fix Serverless JavaScript example (#86)
Browse files Browse the repository at this point in the history
* Update JavaScript example so it works

OpenSearch Service ignores the body and doesn't want it signed - need to sign without the body before adding the body back into the request

* Change signedRequest to request

Co-authored-by: Liz Snyder <[email protected]>
  • Loading branch information
makit and lizsnyder authored Jan 12, 2023
1 parent 25c6607 commit fc295f1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions doc_source/serverless-clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,20 @@ var client = new Client({
request.service = 'aoss';
request.region = ''; // e.g. us-east-1
var contentLength = '0';
if (request.headers['content-length']) {
contentLength = request.headers['content-length'];
request.headers['content-length'] = '0';
}
// You can't include Content-Length as a signed header, otherwise you'll get an invalid signature error.
// The body and content-length should be removed before signing to avoid the header being included
// and the signature being incorrectly calculated including the body.
var body = request.body;
request.body = undefined;
delete request.headers['content-length'];
// Serverless collections expect this header in all requests with an HTTP body. The payload doesn't need to be signed; any value is fine.
request.headers['x-amz-content-sha256'] = 'UNSIGNED-PAYLOAD';
request = aws4.sign(request, AWS.config.credentials);
request.headers['content-length'] = contentLength;
// Add the body back into the request now the signature has been calculated without it
request.body = body;
return request
}
Expand Down Expand Up @@ -378,4 +383,4 @@ func main() {
fmt.Print(resp.Status + "\n")
}
```
```

0 comments on commit fc295f1

Please sign in to comment.