Home > Blockchain >  I am unable to convert output to Json using Ruby and Rspec
I am unable to convert output to Json using Ruby and Rspec

Time:10-07

I'm using a public api from https://regres.in, I've tried several examples but still haven't been successful. It is also not possible to list the body of a get of all users and it is not possible to return the name of a user. Can someone help me please?

my code:

    module User
      include HTTParty
      base_uri 'https://regres.in'
      format :json
    
    end
    
    
    describe 'grouping GET verb methods' do
    
      it ' List all users' do
        @users = User.get('/api/users?page=2')
        puts expect(@users.code).to eq(200)
        puts print @users // I had an error here
        puts @users.code
      end
    
      it ' List one user' do
        @user = User.get('/api/users/2')
        expect(@user.code).to eq(200)
        expect(@user['data']['id']).to eq(2) // I had an error here
        expect(@user['data']['first_name']).to eq("Janet")
      end
    
    end
    
    ###### error ##############
        Failure/Error: puts expect(@user['data']['id']).to eq(2)
        
             JSON::ParserError:
               809: unexpected token at '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
               <html>
               <head><script type="text/javascript">var abp;</script><script type="text/javascript" src="http://ww17.regres.in/px.js?ch=1"></script><script type="text/javascript" src="http://w
        w17.regres.in/px.js?ch=2"></script><script type="text/javascript">function handleABPDetect(){try{if(!abp) return;var imglog = document.createElement("img");imglog.style.height="0px";im
        glog.style.width="0px";imglog.src="http://ww17.regres.in/sk-logabpstatus.php?a=YktuT09HMnIzSTVoNm1UZi9xanNYdEg2a0JMSmFVU2NoazdwTE84ZVMrdTZkQVd3N0o4YlFMQU9EQUY5WklxQlRpcGVuNVpaeUF2ZmZRZ
        
     [![enter image description here][1]][1]         

  [1]: https://i.stack.imgur.com/FYJR2.png

CodePudding user response:

Are you sure it's that basic uri? Googling I found a public API with base uri as "https://reqres.in" with "q"

  • Related