I am using an API to post request, and i received a response from API, but then this API will continue send POST request to webhook, how can i capture the response from Webhook.
def request_api(number)
url = 'http://localhost:3550/call'
#response = HTTParty.post(
url,
:body=> { :phone => "#{number}",
:webhookURL => "http://localhost:3550/call"}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Authorization' => '77d22458349303990334xxxxxxxxxx'
}
)
render json: #response
end
I can see the POST request sent by API in server.ts, and there is a status, how can i capture this status in rails.
POST http://localhost:3550/call (id=1267 status=completed) - received response code=200
Try render json: { status: :ok } instead of render json: #response to send back a response to the API that your server has received webhook successfully. That way you can prevent API keep sending you same webhook again and again.
Use params to capture the response received with the webhook.
Related
I'm new to Rails.
How should I send my update request to the rails server?
I understand that sending post request to /api/user create user.
Shuold I send post request to /api/user/id to update usr's name?
For example:
I want to update UserA's name.
My routes:
/api/user(.:format)
/api/current_users#update {:format=>:json}
// controller/User.rb
def update
#current_user.attributes = user_params
render json: { errors: #current_user.errors.full_messages }, status:
end
Below is my client code with Rxjs(Create request):
exhaustMap(({ payload }) => request({
url: 'users',
method: 'post',
data: {
user: {
email: payload.email,
password: payload.password,
device_token: device_token,
sign_up_with: 'email_and_password',
},
},
So my question is, Can I update User by sending a POST request?
Should I do something else like POST update method???
I am trying to setup Facebook's webhook, and when specifying the callback url, they send a get request to it and expect to receive a code.
I defined an action:
def webhook
render json: { code: '1234567890' }
end
So the reponse to the request is { code: '1234567890' } when they are expecting '1234567890'.
I tried to only have:
def webhook
render json: {'1234567890'}
end
But I get an error.
I also tried to use the head method but nothing is sent back.
How can I only send back an integer ?
As Nani pointed it out in the comments:
def webhook
render :json => 1826416246
end
Works fine !
I am sending HTTP POST request:
data = { id: 1, name: 'ABC' }
uri = URI.parse('http://localhost:3000/events')
http = Net::HTTP.new(uri.host, uri.port)
http.post(uri, data.to_json)
My routes file:
Rails.application.routes.draw do
post '/events' => 'receiver#create'
end
Whereas create methods looks like:
def create
package = {
id: ,
name:
}
end
How can I get access to data values passing via request? My goal is to assign it to new hash within create method
You should set Content-Type header in your request as well:
http.post(uri, data.to_json, {"Content-Type" => "application/json", "Accept" => "application/json"})
Then you can use params[:id] and params[:data] in your controller.
def create
#package = {id: params.require(:id), data: params.require(:data)}
head :no_content
end
head :no_content will render empty reply (so you can create view later). And params.require will ensure that your parameters is provided (otherwise controller will return 400 Bad Request response) – so you won't receive some strange undefined method ... of nil errors later in case you missed params in your request.
I am working with rails application. In my development environment an API makes a HTTP POST request to my application and the application have to respond it accordingly. Every thing is working just fine up to receive the request and respond it with the status 200.
But the response is not delivered successfully to the requesting API.
Here is my code to respond the request
def mysomething
if params[:secret] != webhook_secret
response = {text: "Invalid webhook secret", status: 401}
else
if params[:event] == 'incoming_message'
content = params[:content];
from_number = params[:from_number];
phone_id = params[:phone_id];
result = {messages: {content: "Thanks for your message!"}}
else
result = {messages: {content: "invalid event"}}
end
response = {json: result.to_json, status: 200}
end
render response
end
Can anyone tell me where is the problem? why it is not forwarded to the API.
THANKS
try this
render :json => response
I am coding in Ruby-on-Rails
I would like to send a http request to another service but not wait for a response.
Pseudocode:
def notification
require 'net/http'
...
# send net/http request
Net::HTTP.post_form(url, params)
render :text => "Rendered quickly as did not wait for response from POST"
end
Is there any way to send the POST request and not wait for a response and just to quickly render the page?
You can try delayed_job. It is mainly used to run processes in background. Once you install delayed_job you can do like this.
require 'net/http'
def notification
...
your_http_request #calling method
render :text => "Rendered quickly as did not wait for response from POST"
end
def your_http_request
# send net/http request
Net::HTTP.post_form(url, params)
#do your stuff like where to save response
end
handle_asynchronously :your_http_request