Posting Ruby data in JSON format with Net/http
Make a request object like so:
request = Net::HTTP::Post.new(uri.request_uri,
'Content-Type' => 'application/json')
request.body = newAcctJson
resp = http.request(request)
Ruby send Json response to http post call
You may change
respond_to do |format|
# ... other formats here ...
format.jsonr do
render :json => {
:status => :ok,
:message => "Success!",
:html => "...insert html..."
}.to_json
end
end
to
content_type :json
{
:status => :ok,
:message => "Success!",
:html => "...insert html..."
}.to_json
It uses content_type
, you can find the doc here.
Using Ruby's Net/HTTP module, can I ever send raw JSON data?
After reading tadman's answer above, I looked more closely at adding data directly to the body of the HTTP request. In the end, I did exactly that:
require 'uri'
require 'json'
require 'net/http'
jsonbody = '{
"id":50071,"name":"qatest123456","pricings":[
{"id":"dsb","name":"DSB","entity_type":"Other","price":6},
{"id":"tokens","name":"Tokens","entity_type":"All","price":500}
]
}'
# Prepare request
url = server + "/v1/entities"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.set_debug_output( $stdout )
request = Net::HTTP::Put.new(uri )
request.body = jsonbody
request.set_content_type("application/json")
# Send request
response = http.request(request)
If you ever want to debug the HTTP request being sent out, use this code, verbatim: http.set_debug_output( $stdout ). This is probably the easiest way to debug HTTP requests being sent through Ruby and it's very clear what is going on :)
Sending json post request in thirdparty api from sketchup
you have 2 methods:
JAVASCRIPT INSIDE THE PAGE
-> send a request in javascript directly inside your webdialog as in a normal html page. For exemple, if you use Jquery:
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
.....
<body>
<button id="save">save</button>
</body>
<script>
$( "#save" ).click(function() {
$.ajax({
type: "POST",
url: "http://127.0.0.1:5000/api/v1/projectStatus/save",
data: {
"length": "11",
"width": "12",
"volume": "168"
},
success: success,
dataType: dataType
});
});
</script>
</html>
IN RUBY
inside your ruby by removing Sketchup ruby code, by removing Sketchup and add
Sketchup::require 'net/http'
....
@dialog.add_action_callback("save") { |action_context, value|
self.update_material(value)
uri = URI("http://127.0.0.1:5000/api/v1/projectStatus/sav")
result_json = Net::HTTP.post(uri,{
"length": "11",
"width": "12",
"volume": "168"
})
result = JSON.parse(result_json)
}
Hope it's help
How to properly format json to send over using RestClient
You should put all your params in one params hash like this:
params = {
recipient: { id: sender },
message: { text: text },
access_token: page_token
}
response = RestClient.post base_uri, params.to_json, content_type: 'application/json', accept: 'application/json'
p "this is the response #{response}"
Sending JSON object in Rails via Post
You want to use from_json
which does the opposite of to_json
. This will take the object being sent in JSON and convert it into ruby so your model or however you save it will understand.
@post = Post.new
@post.name = "Hello"
#this converts the object to JSON
@post.to_json
@post = You want to use `from_json` which does the opposite of `to_json`. This will take the object being sent in JSON and convert it into ruby so your model or however you save it will understand.
@post = Post.new
@post.name = "Hello"
#this converts the object to JSON
@post.to_json
@post = {
"name" => 'Hello'
}
#this puts it back into JSON
@post.from_json
@post.name = "Hello"
@post.to_json
In terms of sending JSON (server side) to a controller that is usually done from JavaScript or AS3.
You should explain how you are posting. From a form, from a link. That matters; however, the easiest way is to use jQuery.
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: json
});
Here you want the url
to be the post url such has app.com/posts (your domain, and then a controller) and then the data should be in JSON format, for example:
data = {
"name" => 'Hello'
}
Related Topics
How to Pick Randomly from an Array
What's the Difference Between Uri.Escape and Cgi.Escape
How to Create Multiple Submit Buttons For the Same Form in Rails
No Increment Operator (++) in Ruby
Best Practices With Stdin in Ruby
How to Modify Path For Homebrew
Do..End VS Curly Braces For Blocks in Ruby
How to Get a Random Number in Ruby
Where and How Is the _ (Underscore) Variable Specified
How to Make a Http Request Using Ruby on Rails
Is 'Eval' Supposed to Be Nasty
Ruby MySQL2 Gem Installation on Windows 7