Ruby uses an external API to create a web application that allows you to check the pickup status of the delivery.
I was able to link the API, but I don't know where and how to fix it because invalid API
is displayed in View.If the code editing method and the external API implementation method are incorrect, please let me know.
`
class CollectingStatusController<ActionController::Base
class Trackingmore
require 'uri'
require 'net/http'
require'net/https'
require 'json'
def tracker(url, postData, method)
headers={'Content-Type'=>'application/json', 'Trackingmore-Api-Key': 'my api key'}
if postData.empty?
@toSend=""
else
@toSend=postData.to_json
end
uri = URI.parse(url)
https=Net::HTTP.new(uri.host,uri.port)
if method=="GET"
req = Net::HTTP::Get.new(uri.path, headers)
elsif method=="POST"
req = Net::HTTP::Post.new (uri.path, headers)
elsif method=="PUT"
req = Net::HTTP::Put.new (uri.path, headers)
elsif method=="DELETE"
req = Net::HTTP::Delete.new (uri.path, headers)
else
puts "parameter method is wrong!"
end
req.body="#{@toSend}"
res = https.request(req)
return res
end
end
end
`
`
<!--[1]Change class and action -->
<form class="index" action="https://api.trackingmore.com/v2" method="post">
<div class="container">
<div class="flex space-between">
<div class="articlesNews-leftContent">
<div class="article-MainBlock">
<form action="/connpass_clients" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden"value=" "">
<div class="flex">
<div class="p-t-b-5"> slip number</div>
<div><input type="text" name="form_words" id="form_words"placeholder="Tokyo Programming">/div>
</div>
<div class="text-10">Multiple searches available in space commas</div>
<div class="flex m-t-b-30">
<div>Delivery date & time</div>
<div><select id="start_date_year" name="start_date[year]">
<option value="2017">2017</option>
<option value="2018" selected="selected">2018</option>
<option value="2019">2019</option>
</select>
<select id="start_date_month" name="start_date[month]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9" selected="selected">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<input type="hidden" id="start_date_day" name="start_date[day]"value="1">
</div>
<div>~</div>
<div><select id="end_date_year" name="end_date [year]">
<option value="2017">2017</option>
<option value="2018" selected="selected">2018</option>
<option value="2019">2019</option>
</select>
<select id="end_date_month" name="end_date[month]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9" selected="selected">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<input type="hidden" id="end_date_day" name="end_date[day]"value="1">
</div>
</div><input type="submit" name="commit" value="Search">
`
ruby ver
2.3.3p222 (2016-11-21 revision 56859)
Rails ver
Rails 5.1.6
I haven't received a reply to your comment yet, but index.html.erb in The biggest problem is this line of This means that the browser sends the request directly to the API server, but the browser does not convert the post request to JSON, nor does it put the API key in the HTTP header.Since you are sending a request without an API key, it appears that the API server has returned a response to the browser with meta information (If you type You are writing the code little by little while checking the movement here and there, but the current code is not enough to test whether the Ruby API call works.You have to write a code that uses the API calling class ( Try replacing the previous line with the following: (The controller name and action name may need to be rewritten depending on the actual web application configuration, but the following assumptions are made.Depending on the actual configuration, it may need to be replaced accordingly.) The browser now sends a request to the However, you should modify the Rails will always have the After this, you must add the The whole thing will look like this. There are many unclear points, and I didn't actually move them to check, so there may be some imperfections, but I think I have included the direction of the correction.I would appreciate it if you could use it as a reference.CollectingStatusController
is assumed to be the view of CollectingStatusController
in and so on.
index.html.erb
.<form class="index" action="https://api.trackingmore.com/v2" method="post">
{"code":4001, "type": "Unauthorized", "message": "Invalid API key"}
.https://api.trackingmore.com/v2
directly into the URL window of your browser, you should see the same response.)Trackingmore
) as the Ruby side code.index.html.erb
...
<form class="index" action="<%=url_for:controller=>'collection_status',:action=>'search'%>"method="post">
...
CollectingStatusController
and requires a method to handle it.Trackingmore
class nested inside the CollectingStatusController
before you touch collecting_status_controller.rb
.app
directory, but create the lib
directory under it (if you already have a directory for similar purposes, you can do so), and then place the trackingmore.rb
on one line below.app/lib/trackingmore.rb
...
headers={'Content-Type'=>'application/json', 'Trackingmore-Api-Key'=>'Actual API Key'}
...
search
method to collecting_status_controller.rb
.(Of course, Trackingmore
class definitions are deleted.)collecting_status_controller.rb
...
class CollectingStatusController<ActionController::Base
def search
# Create postData by extracting parameters from params (validate and process errors if necessary)
#post_data=...
# Prepare the URL of the service you want to use as a string
url='https://api.trackingmore.com/v2/trackings/updatemore'
# The way of calling on the reference page is almost the same.
tracker = Trackingmore.new
res=tracker.tracker(url, post_data, 'POST')
# Also check res.code and if there are no errors, analyze res.body as JSON.
#json=...
# You may need to check if the contents of json are errors.
#...
# Create data to pass from analyzed JSON to view
# @search_results=...
# Display view for display
end
end
post_data
is quite difficult to create request parameters, but if you just want to check if the API is calling well, you can create dummy data for now.You will also need to create a result-checking view (search.html.erb
), but it depends on what you want to see, so I'll leave it here.
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
578 Understanding How to Configure Google API Key
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.