Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a JSON as POST parameter

I have a ruby application and I need to receive a JSON from a client. Receiving a JSON is just like receiving a string? I just have to do something like:

information = params[:json_data]
data_parsed = JSON.parse(information)

That's all or I have to do something different when getting a JSON? The sender has to send me that like string?

Thanks!

like image 572
Andres Avatar asked Aug 31 '12 19:08

Andres


People also ask

How do I get JSON POST data?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

Can I send JSON in GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.

How do you pass JSON data in a POST request in python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

How do I POST JSON to a REST API endpoint?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.


2 Answers

What you are describing is OK, but it implies that there is a param named json_data. If you instead mean that the entire POST body is nothing but the JSON, then you want to look at request.raw_post. You'd end up with something like this:

information = request.raw_post
data_parsed = JSON.parse(information)
like image 59
jdl Avatar answered Oct 17 '22 04:10

jdl


The official document says,

If the "Content-Type" header of your request is set to "application/json", Rails will automatically load your parameters into the params hash, which you can access as you would normally.

So for example, if you are sending this JSON content:

{ "company": { "name": "acme", "address": "123 Carrot Street" } }

Your controller will receive params[:company] as { "name" => "acme", "address" => "123 Carrot Street" }.

Also, if you've turned on config.wrap_parameters in your initializer or called wrap_parameters in your controller, you can safely omit the root element in the JSON parameter. In this case, the parameters will be cloned and wrapped with a key chosen based on your controller's name. So the above JSON POST can be written as:

{ "name": "acme", "address": "123 Carrot Street" }

And, assuming that you're sending the data to CompaniesController, it would then be wrapped within the :company key like this:

{ name: "acme", address: "123 Carrot Street", company: { name: "acme", address: "123 Carrot Street" } }

So, if you send/POST { "hello": "world"} to apples/, then params['apple'] will be the object for the Json payload.

class ApplesController < ApplicationController
  def create
    # params['apple'] is the object represents Json payload
  end
end
like image 30
Hong Avatar answered Oct 17 '22 04:10

Hong