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)

class Crud
  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 }")
  end 
  
def delete(id)
self.class.delete("/delete/#{ id }")
end


  $response = @manter_user.create
  expect(@manter_user.create.code).to eq (200)
  puts $response.body
  pp @id = JSON.parse($response)["id"]
  *no implicit conversion of HTTParty::Response into String (TypeError)*
  puts @manter_user.retrieve(@id)
  expect(@manter_user.retrieve(@id).code).to eq (200) 
  • 2
    This is a duplicate of your last question: [No implicit conversion of HTTParty::Response into String (TypeError) api with Ruby](https://stackoverflow.com/questions/75025731/no-implicit-conversion-of-httpartyresponse-into-string-typeerror-api-with-ru) – halfer Jan 10 '23 at 17:27
  • Could you reply to @javiyu on the duplicate question? They had some advice on `$response` vs. `$response.body`. – halfer Jan 10 '23 at 17:29
  • Nobody answers me!! – Cassia Klaison Jan 10 '23 at 17:30
  • I have repaired the readability of this question twice, and on each occasion, the improvement has been reverted. Please see the revision history: https://stackoverflow.com/posts/75073592/revisions – halfer Jan 10 '23 at 17:30
  • Worth a read: https://stackoverflow.com/help/no-one-answers – halfer Jan 10 '23 at 17:34
  • Regarding your latest edit, now the Markdown code formatting is broken. – halfer Jan 10 '23 at 17:34
  • They are only correcting my texts but no one has overcome my problem so far – Cassia Klaison Jan 10 '23 at 17:34
  • 1
    @CassiaKlaison change `JSON.parse($response)["id"]` to `JSON.parse($response.body)["id"]` like [javiyu](https://stackoverflow.com/users/3994337/javiyu) mentioned in your other post. – engineersmnky Jan 10 '23 at 17:40
  • I can't presently find the guidance on Meta Stack Overflow, but the correct response to not getting an answer is _not_ to duplicate the question. That just creates new work for a volunteer to clean up the duplicate. Please just ask questions once. – halfer Jan 10 '23 at 17:41
  • I've tried it and it doesn't work!!.. I'm tired of it!! – Cassia Klaison Jan 10 '23 at 17:45
  • Can't make heads or tails of the code posted. If `$response.body` is the JSON shown it's unclear what the issue is--the record is in `"data"`, the ID is in `"data.id"`. – Dave Newton Jan 10 '23 at 17:49
  • Edit your other question and show what is in `$response.body`. Then ping @javiyu for further help. Perhaps the JSON is invalid, and thus `JSON.parse()` cannot create a data structure from it. – halfer Jan 10 '23 at 17:51
  • It used to work perfectly, now it doesn't. – Cassia Klaison Jan 10 '23 at 18:04
  • I posted my entire code, see if it's clearer. I manage to create the record but when querying the same id I get this error type problem – Cassia Klaison Jan 10 '23 at 18:09
  • Presumably this code won't even compile with `*no implicit conversion of HTTParty::Response into String (TypeError)*` in the middle of it. Please edit the question to repair this. – halfer Jan 10 '23 at 18:10
  • I don't do Ruby, but I assume the `class` needs terminating, perhaps with an `end` statement. Please add that, in the appropriate place. – halfer Jan 10 '23 at 18:12
  • yes, i know about that. I think is there som problem with the gem httparty. – Cassia Klaison Jan 10 '23 at 19:32
  • The error you are getting is because `$response.is_a?(HTTParty::Response)` you have not shown any attempt to change this to `response.body` which I believe HTTParty parses for you any way. Can you post the output provided by `puts $response.body` – engineersmnky Jan 10 '23 at 21:09
  • @engineersmnky do not work!!... – Cassia Klaison Jan 10 '23 at 21:19
  • @engineersmnky When I query it in postman it returns blank date. In ruby {"status":"success","data":{"name":"Fermina","salary":4759.4300000000002910383045673370361328125,"age":18,"id":9951},"message":"Successfully! Record has been added."} no implicit conversion of HTTParty::Response into String (TypeError) In Postman {"status":"success","data":null,"message":"Successfully! Record has been fetched."} – Cassia Klaison Jan 10 '23 at 21:22
  • @CassiaKlaison please try this: `pp @id = JSON.parse!($response.body)["id"]`; if the `json` is invalid, ruby will raise an error. if not, this might work – manuwell Jan 10 '23 at 21:29
  • $response.parsed_response["id"] {"status":"success","data":null,"message":"Successfully! Record has been fetched."} pp @id = JSON.parse!($response.body)["id"] expected: 200 Nothing works like it used to. Can I put my project on github and send it to you to look at?got: 429 – Cassia Klaison Jan 10 '23 at 22:13
  • That is a public api.. https://dummy.restapiexample.com/ – Cassia Klaison Jan 11 '23 at 12:41
  • my github https://github.com/sqkassia/testapi If you can help me I'll pay if you want. – Cassia Klaison Jan 11 '23 at 14:18

0 Answers0