#Rest API in CURL

2 messages · Page 1 of 1 (latest)

calm hill
#

When I use the following CURL command, it does not populate the version field. Do you have any ideas why?

Request:

curl -H "Authorization: Bearer <bearer_token>" -F 'version=1.0.0' -X POST http://cms.calgo.io/items/releases

Response:

{
  "data":
    {
      "id":"a7b0eaf3-1741-4907-96f3-ba7bb675e021",
      "date_created":"2022-11-04T20:58:59.038Z",
      "file":null,
      "version":null
    }
}
calm hill
#

Further testing shows even more strange stuff going on. Switching to Invoke-WebRequest in PowerShell to avoid any encoding issues. First, I upload a file:

$file = Get-Item -Path ".\image.png"
$fileForm = [ordered]@{ title="My Image"; file=$file }
$fileRequest = Invoke-WebRequest -Method POST -Uri "${baseUri}files" -Headers $headers -ContentType multipart/form-data -Form $fileForm | ConvertFrom-Json

That result is OK, and I get back the file id at $fileRequest.data.id. Let's try to post a custom data model release with version and file properties.

$releaseForm = @{ version="1.0.0"; file=$fileRequest.data.id }
$releaseRequest = Invoke-WebRequest -Method POST -Uri "${baseUri}items/releases" -Headers $headers -ContentType multipart/form-data -Form $releaseForm | ConvertFrom-Json

Returns an error:

Invoke-WebRequest:{"errors":[{"message":"\"file\" is required","extensions":{"code":"FAILED_VALIDATION","field":"file","type":"required"}},{"message":"\"version\" is required","extensions":{"code":"FAILED_VALIDATION","field":"version","type":"required"}}]}

However, if I convert it to JSON and pass it as a body:

$releaseRequest = Invoke-WebRequest -Method POST -Uri "${uri}items/releases" -Headers $headers -ContentType application/json -Body ($releaseForm | ConvertTo-Json) | ConvertFrom-Json

Then I get OK status?!