HTTP 400 Bad Request when calling OCI REST due to "Unable to parse message body"
I was able to successfully use the instructions in a blog post titled Oracle Cloud Infrastructure (OCI) REST call walkthrough with curl written by the Oracle A-Team to call an OCI REST service to create an Autonomous Database without using oci-curl.
I was able to use the bash script (no longer available on the page, but a sample provided here) that adds necessary headers in the POST request and creates a signing string prior to calling curl. But that is all a topic for a different blog post.
This was my payload used to to create an Autonomous Database
{
"compartmentId" : "ocid1.tenancy.oc1..aaaaaaaamvsnb6fsq6ynaxtpsq"
"displayName" : "Live Demo",
"dbName" : "AHMEDDB",
"adminPassword" : "Kobe_24_24_24",
"cpuCoreCount" : 1,
"dataStorageSizeInTBs" : 1
}
When I ran the script, I encountered this error:
root@dev:/root/ocitemp> ./createdb.sh
===============================================================================
signing string is (request-target): post /20160918/autonomousDatabases
date: Tue, 12 Dec 2023 03:48:28 GMT
host: database.us-ashburn-1.oraclecloud.com
x-content-sha256: WnYlVLJ5xLgmKI0o64G52BVSc2GK69WgZTW07T2TLi4=
content-type: application/json
content-length: 297
Enter pass phrase for /root/ocitemp/oci.pem:
Signed Request is
uQ/13u3lubtp79N9OAS3aojahQ13oExTtrNZckm34wDYkLa...
===============================================================================
+ curl -v -X POST --data-binary @request.json -sS https://database.us-ashburn-1.oraclecloud.com/20160918/autonomousDatabases -H 'date: Tue, 12 Dec 2023 03:48:28 GMT' -H 'x-content-sha256: WnYlVLJ5xLgmKI0o64G52B' -H 'content-type: application/json' -H 'content-length: 297' -H 'Authorization: Signature version="1",keyId="ocid1.tenancy.oc1..aaaaaaaamvsnb6fteatsnynaxtpsq/ocid1.user.oc1..aaaaaaaaqtpmtdoc7664vcc6ywpoasdppgdjq/5d:2b:a5:aa::d2:9f:d8:46:7a",algorithm="rsa-sha256",headers="(request-target) date host x-content-sha256 content-type content-length",signature="uQ/13u3lubtp79NPA5bQiqJregZ5/Be4c6sGbf6sml+25ubkza6Plbw=="'
* About to connect() to database.us-ashburn-1.oraclecloud.com port 443 (#0)
* Trying 140.91.12.32...
* Connected to database.us-ashburn-1.oraclecloud.com (140.91.12.32) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* subject: CN=*.us-ashburn-1.oraclecloud.com,O=Oracle Corporation,L=Redwood City,ST=California,C=US
* start date: Jun 08 00:00:00 2023 GMT
* expire date: Jun 07 23:59:59 2024 GMT
* common name: *.us-ashburn-1.oraclecloud.com
* issuer: CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1,O=DigiCert Inc,C=US
> POST /20160918/autonomousDatabases HTTP/1.1
> User-Agent: curl/7.29.0
> Host: database.us-ashburn-1.oraclecloud.com
> Accept: */*
> date: Tue, 12 Dec 2023 03:48:28 GMT
> x-content-sha256: WnYlVLJ5xLgmKI0o64G52BVS7T2TLi4=
> content-type: application/json
> content-length: 297
> Authorization: Signature version="1",keyId="ocid1.tenancy.oc1..aaaaaaaamvsnb6xljssq6ynaxtpsq/ocid1.user.oc1..aaaaaaaaqtpmtdoc7664vcc34poasdppgdjq/5d:2b:d2:9f:d8:46:7a",algorithm="rsa-sha256",headers="(request-target) date host x-content-sha256 content-type content-length",signature="uQ/13u3lubtp79N9OAS3aojahQ13oExTtrN3URO6sGbf6sml+25ubkza6Plbw=="
>
* upload completely sent off: 297 out of 297 bytes
< HTTP/1.1 400 Bad Request
< Date: Tue, 12 Dec 2023 03:48:31 GMT
< opc-request-id: /62CDD05D684809/E9C168528D409992
< Content-Type: application/json
< Strict-Transport-Security: max-age=31536000; includeSubDomains;
< Content-Length: 79
<
{
"code" : "InvalidParameter",
"message" : "Unable to parse message body"
* Connection #0 to host database.us-ashburn-1.oraclecloud.com left intact
Note the error HTTP 400 and the JSON response message from the output above:
< HTTP/1.1 400 Bad Request
"code" : "InvalidParameter",
"message" : "Unable to parse message body"
It look me a while to realize that I was missing a comma in my input JSON payload:
BAD:
"compartmentId" : "ocid1.tenancy.oc1..aaaaaaaamvsnb6fsq6ynaxtpsq"
GOOD:
"compartmentId" : "ocid1.tenancy.oc1..aaaaaaaamvsnb6fsq6ynaxtpsq",