All threads / Can some explain this line of code

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Can some explain this line of code

hkauroberoi asked in Ruby

Hello,

I'm following a RUBY code and writing a small script in php that will accept post request (json) and show values. However, the following line puzzles me - what does params["payload"] is doing ?

params = CGI::parse(post_body)
digest = Digest::SHA1.hexdigest(params[“payload”].to_json+secret_key)

{
"payload": {
"useriden": "900af657a65e",
"score": 50,
"Average": 25
},
"signature": "af886289"
}

Hi,

params is a json-blob that "basically" comes from the users browser. So for a HTTP GET request, if someone visits a URL like: /users?something=test&name=john - then params will look like:

{
something: "test",
name: "john"
}

The same basically goes for a HTTP POST request.

So params["payload"] in your example, references the content within:
"payload": {
"useriden": "900af657a65e",
"score": 50,
"Average": 25
},

so { useriden: "900af657a65e", score: 50, average: 25 }

I hope that makes sense? It's essentially data that hits the ruby backend via a HTTP request.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.