-1

I get a JSON array from another app which looks like this (from the log)

Parameters: {"auth_token"=>[{"auth_token"=>"d11D1E3v7pKkLW4wDj8ohgtf"}]}

This array will be bigger (that's the reason for using arrays) but I'm only testing if I can get the content from it. This is my function:

def valid_request_array
    data = ActiveSupport::JSON.decode(params)[:auth_token]
    Usuario.find_by_auth_token(data[0]['auth_token']).present?
end

I get the following error:

TypeError (no implicit conversion of ActionController::Parameters into String): app/controllers/api/actas_controller.rb:35:in `valid_request_array'

Line 35 is data = ActiveSupport::JSON.decode(params)[:auth_token]

I have rails 4.2 and also tried with JSON.parse(params) and JSON.parse(params[:auth_token]) but it doesn't works. There's a similar question here JSON Array parsing in ROR but I need to read the value in order to authenticate that request. Any help is appreciated

2 Answers 2

1

Since this is Params, you don't need to decode or parse it. You will only need this if what you are operating on is a jasonfied (or stringified) object. eg is something like this:

"{\"auth_token\":[{\"auth_token\":\"d11D1E3v7pKkLW4wDj8ohgtf\"}]}" 

then you can parse this to get your hash object back.

In this case you don't need to, since the object is not stringified. That is why you are having that error:

no implicit conversion of ActionController::Parameters into String

to get the token however, just call it directly as:

params["auth_token"].first["auth_token"]

that should return the token d11D1E3v7pKkLW4wDj8ohgtf for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Both answers work, but I'm gonna accept this one because the answer is a little more extense. Thank you, I was stuck with this all afternoon
0

You are getting this error because the params is not a string, whereas decode expects a string. The params that you are getting is not in a JSON format. Try:

def valid_request_array
   Usuario.find_by_auth_token(params[:auth_token][0]["auth_token"]).present?
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.