terraform-provider-restapi icon indicating copy to clipboard operation
terraform-provider-restapi copied to clipboard

Not Able to create resource using this provider. getting unexpected end of JSON input even though JSON is valid

Open kanwar25 opened this issue 2 years ago • 6 comments

Hi, I am facing Error: unexpected end of JSON input when trying to create a resource. JSON passed is a valid JSON

Please check what I am doing wrong

resource "restapi_object" "update_meta_data_monitor" { provider = restapi.dynatrace path = "/api/v1/synthetic/monitors" create_method = "PUT" update_method = "PUT" create_path = "/api/v1/synthetic/monitors/{id}" object_id = "SYNTHETIC_TEST-4EC510686B6A48E1" debug = true data = "{"anomalyDetection":{"loadingTimeThresholds":{"enabled":true,"thresholds":[{"requestIndex":1,"type":"TOTAL","valueMs":100}]},"outageHandling":{"globalOutage":true,"localOutage":true,"localOutagePolicy":{"affectedLocations":1,"consecutiveRuns":3}}},"enabled":true,"frequencyMin":5,"keyPerformanceMetrics":{"loadActionKpm":"VISUALLY_COMPLETE","xhrActionKpm":"VISUALLY_COMPLETE"},"locations":["GEOLOCATION-9999453BE4BDB3CD"],"name":"BrowserMonitorExample","script":{"configuration":{"device":{"deviceName":"Desktop","orientation":"landscape"}},"events":[{"description":"Loadingof","type":"navigate","url":"example.com","wait":{"waitFor":"page_complete"}}],"type":"availability","version":"1.0"},"type":"BROWSER"}" }

kanwar25 avatar Jun 14 '22 19:06 kanwar25

@DRuggeri

(https://github.com/Mastercard/terraform-provider-restapi/commits?author=DRuggeri)This is the same issue which I am facing https://github.com/fmontezuma/terraform-provider-restapi/pull/1

kanwar25 avatar Jun 15 '22 18:06 kanwar25

Thanks, @kanwar25 for the report. Can you share more information leading up to the error. If you are correct that this is the same problem as referenced, it'd make sense to close this issue and follow the PR. Note: in that PR, there may be some concerns - I'll add them in the discussion there

DRuggeri avatar Jun 27 '22 17:06 DRuggeri

https://github.com/Mastercard/terraform-provider-restapi/pull/140 - This is the same issue with my REST API PUT Call, When a PUT type request was made and the response was empty, the terraform resources presented the error "invalid JSON: unexpected end of JSON input".

kanwar25 avatar Jun 27 '22 18:06 kanwar25

Is there a way to upvote this feature? Even I have an API that responds with an empty 200 on a PUT.

arun-a-nayagam avatar Aug 05 '22 16:08 arun-a-nayagam

Same issue here. API responds with an empty 200 and the object_id is set on resource level, so there is no reason to check the response and fail with the above error. I guess this would be a resource-based override of the write_returns_object and create_returns_object set on the provider.

thomasnowotny avatar Sep 12 '22 13:09 thomasnowotny

In our case the API returns 204 which is always no content, so it doesn't make sense to expect a body in response. Maybe a fix can be made for at least that part?

kasparasgecas avatar Sep 23 '22 12:09 kasparasgecas

any update here, i am also facing same problem.

jainbhavya65 avatar Apr 06 '23 15:04 jainbhavya65

Not sure if this is the best place or if I should comment on the PR or open a new issue, but...

@alahijani I was trying to test your change locally b/c if the fix works I was going to publish my own version of the provider to Terraform Cloud/Enterprise and reference that.

I was able to get it to build on my mac and using the docker image / run command listed https://github.com/Mastercard/terraform-provider-restapi#development-environment-requirements

I'm building using go buiild from the top directory (I also tried go install .). I was able to setup the binary in for example, the test I was doing on my mac I was using ls ~/.terraform.d/plugins/example.com/test/restapi/1.18.2/darwin_arm64/terraform-provider-restapi I know its not the best name but I can alway update that later.

Here's my provider setup:

terraform {
  required_version = "1.2.4"

  required_providers {
    restapi = {
      version = "~> 1.18.2"
      source = "example.com/test/restapi"
    }
  }
}

provider "restapi" {
 uri = "http://localhost:53049" 
 write_returns_object = false
 create_returns_object = false
 debug = true
 headers = {
 "Authorization" = "Bearer ${local.auth_token}",
 "Content-Type" = "application/json"
 }
 create_method = "PUT"
 update_method = "PUT"
 destroy_method = "PUT"
}

and the resource / api call is:

resource "restapi_object" "system_settings_setup" {
  object_id = "systemsettingsetupid"
  path = "/api/v1/settings/system/system"
  data = file("${path.root}/files/system_settings.json")
  debug = true
}

When I try to apply the restapi_object I get the following error. Its worth noting when I ran go build locally it created the binary but it didn't see any output to STDOUT, I also tested I ran go build from the container (after cloning that get repo bc go get didn't work). Both created a binary even though there wasn't output. I was just first testing with the one I can run locally on my laptop (mac).

The error I'm getting when applying is:

restapi_object.system_settings_setup: Creating...
╷
│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to restapi_object.system_settings_setup, provider "provider[\"example.com/test/restapi\"]" produced an unexpected new value: Root resource was present, but now absent.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.

bradenwright-opunai avatar Aug 29 '23 01:08 bradenwright-opunai

Any thoughts or updates on https://github.com/Mastercard/terraform-provider-restapi/issues/178#issuecomment-1696624995

bradenwright-opunai avatar Sep 07 '23 04:09 bradenwright-opunai

Still looking for details on this

bradenwright-opunai avatar Sep 12 '23 18:09 bradenwright-opunai