0

This was running without problems, now I'm getting the following error when I use the same ID that I registered in the query:

no implicit conversion of HTTParty::Response into String (TypeError)

Code:

include HTTParty
base_uri 'http://dummy.restapiexample.com/api/v1'

def create 
    nome = Faker::UniqueGenerator.clear
    nome = Faker::Name.unique.first_name
    salario = Faker::Number.number(digits: 2)
    idade   = Faker::Number.number(digits: 2)
    $body = {name: nome, salary: salario, age: idade }.to_json
  
  $headers = {
    'Accept' => 'application/vnd.tasksmanager.v2',
    'Content-Type' => 'application/json'
  }

  self.class.post('/create', body: $body, headers: $headers) 
end

def retrieve(id)
  self.class.get("/employee/#{ id }")
 
 
 When("the request to change the employee is sent") do
  $response = @manter_user.create
  expect(@manter_user.create.code).to eq (200)
  puts $response.body
  {"status":"success","data":{"name":"Patrice","salary":59,"age":39,"id":364},"message":"Successfully! Record has been added."}

  
  @id = JSON.parse($response)['id'] #### error
  puts @manter_user.retrieve(@id)
  no implicit conversion of HTTParty::Response into String (TypeError)
  expect(@manter_user.retrieve(@id).code).to eq (200) 

end
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Tip: Stay away from *global variables* like `$response`. This isn't PHP. Ruby variables are just `response = ...` – tadman Jan 06 '23 at 04:35
  • Which line is failing? – tadman Jan 06 '23 at 04:37
  • 3
    Probably you meant to do `JSON.parse($response.body)` and not `JSON.parse($response)`. – javiyu Jan 06 '23 at 06:48
  • This was working before, my code I create and query the same ID $response = @manter_user.create expect(@manter_user.create.code).to eq (200) puts $response.body puts @manter_user.retrieve(@id) expect(@manter_user.retrieve(@id).code).to eq (200) – Cassia Klaison Jan 06 '23 at 12:12

1 Answers1

2

Since you provided many additional details in your other Post I built a working example for your to review: https://replit.com/@engineersmnky/APIEXAMPLE#main.rb

Just to wrap this up:

Your error is caused by this line

@id = JSON.parse($response)

The $response variable is an instance of HTTParty::Response however JSON.parse is expecting a String.

To solve this you can use $response.body which will return a JSON formatted String for parsing. Additionally HTTParty will auto parse many known formats so you could skip this step and simply use $response.parsed_response which will return the same.

Your second issue is that you are trying to access "id" in the wrong location. Your response is formatted as:

resp = {"status":"success",
        "data":{
          "name":"Patrice",
          "salary":59,
          "age":39,
          "id":364},
        "message":"Successfully! Record has been added."}

Here you can see "id" is nested under "data" but you are trying to access it at the top level:

resp["id"] 
#=> nil 
resp["data"]["id"] 
#=> 364

IMPORTANT NOTE: I certainly understand how frustrating being a new programmer can be and how that can impact your attitude and approach, but please try to remember you came to SO for help (and lots of commenters tried to help). If you had taken a minute to listen to the comments, with a clear, calm mind, you would see that they were correct about why your error was occurring and if you had tried to implement their changes you would see everything works exactly as expected. I hope this helps you now and in the future. Best of luck!

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • 1
    Bravo; kudos to you. – Dave Newton Jan 11 '23 at 18:12
  • 1
    Very grateful, I confess that I was confused because it was working perfectly. Perhaps there must have been some changes in Ruby regarding Gems. I'm working with CodeCeptJS to automate and wanted to revive this code. Thank you very much for your words. – Cassia Klaison Jan 11 '23 at 20:54
  • One more thing, I wanted to list the entire record after being updated, also showing the same ID that I show in the creation to prove that it is the same updated ID. CREATE: The created employee data are: {"status":"success","data":{"name":"Nell","salary":4462.23,"age":14,"id":5392},"message":"Successfully! Record has been added."} Update The created employee data are: {"status":"success","data":{"name":"Grace","salary":"4500,28","age":"36"},"message":"Successfully! Record has been updated."} – Cassia Klaison Jan 11 '23 at 21:07
  • my code: @manter_user = Crud.new create_response = manter_user.create puts "Create Response HTTP code 200: #{create_response.code == 200}" puts "The created employee data are:: #{create_response}" id = create_response.parsed_response["data"]["id"] puts "id: #{id} updated is the same as previously created...." puts retrieve_response = manter_user.update(id) puts "Update Response HTTP code 200: #{retrieve_response.code == 200}" – Cassia Klaison Jan 11 '23 at 21:14
  • @CassiaKlaison unfortunately that API does not appear to fully support its own implementation. You can "create" and it will respond, you can retrieve ids up to 24, You can update but the response is malformed, and you can "delete" using any number. Not much you can do about this it's just broken. I updated my example to show you better. – engineersmnky Jan 11 '23 at 21:31
  • Thanks again!!!! you have a good heart!! – Cassia Klaison Jan 12 '23 at 12:47
  • @engineersmnky my best friend I posted a new problem that if I solve it, I'll finish everything. I want to use the same ID to list, change and remove, I'm getting error code 200. $manter_user = Crud.new $response = $manter_user.create puts "Response is: #{$response}" puts "Create Response HTTP code 200: #{$response.code == 200}" #List $id = $response.parsed_response["data"]["id"] puts "@id: #{$id} returned is the same previously created..." retrieve_response = $manter_user.retrieve($id) puts "Retrieve Response HTTP code 200: #{retrieve_response.code == 200}" end – Cassia Klaison Jan 13 '23 at 19:09