Rails 3.2.8 - How to receive image, video and sound file without form on Rails? - ruby

I'm making an API that deals with mobile devices interactions.
I know how to deal with simple variables handling such as this:
curl -XPOST -H "Content-Type: application/json" "http://localhost:3000/api/v1/registrations" -d "{registration: {"nickname":"john_doe", "password":"secret", "confirmed":"true", "email":"john.doe#gmail.com","blood_type":"B+"}}"
This POST call will go to my controller:
class RegistrationController < ApplicationController
def create
Registration.create!(params[:registration])
end
end
It's just a very simple way to post information to my Registration model.
However, if I'm handling image, sound and video upload using a JSON through a POST request, besides having to use an uploaded like Carrierwave, is there anything else I need to do?
For example, would it work a request like this?
curl -XPOST -H "Content-Type: application/json" "http://localhost:3000/api/v1/registrations" -d "{registration: {"nickname":"john_doe", "password":"secret", "confirmed":"true", "email":"john.doe#gmail.com","blood_type":"B+", "image": IMAGE_FILE_IN_BINARY_OR_SOME_OTHER_FORMAT?}}"
Should the image file be trasmitted as a binary or there's a specific format for it?

Related

youtube data api search by viewCount wrong results

When I start a GET request to https://youtube.googleapis.com/youtube/v3/search?part=snippet&channelId=*channelId*&order=viewCount&key=*apiKey* I am supposed to get the most viewed videos of the channel.
In the response's items array's first element is a 6 months old video with around 4k views.
On the channel's Youtube page if I sort by most viewed, the first one is 10 years old with 200k views.
Any idea why I do not get the correct results?
I double-checked, the channel id is correct.
Easy workaround:
Use my YouTube operationnal API, make a GET request to https://yt.lemnoslife.com/search?part=snippet&channelId=*channelId*&order=viewCount
A bit more complicated workaround:
curl -s 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' -H 'Content-Type: application/json' --data-raw '{"context":{"client":{"clientName":"WEB","clientVersion":"2.9999099"}},"browseId":"CHANNEL_ID","params":"EgZ2aWRlb3MYASAAMAE="}'
Don't forget to replace CHANNEL_ID with the channelId you want the most viewed videos from.
If you need more than the first 30 results, grab the continuation token from the response of the request above and use the following command (and if you're still interested for more results use the last continuation token you got):
curl -s 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' -H 'Content-Type: application/json' --data-raw '{"context":{"client":{"clientName":"WEB","clientVersion":"2.9999099"}},"continuation":"CONTINUATION_TOKEN"}'
Don't forget to replace CONTINUATION_TOKEN with the continuation token you got from the latest request reponse.
Note: don't pay attention to the key, it's not a Youtube Data API v3 key.

Tricks to figure out how to login to Dexcom's API to get blood sugars?

Any way to figure out tricks to login to an API by Dexcom? This article discusses an approach for accessing blood sugar values but the first step is to log into the API and it is unclear how that step was taken as this is an undocumented API.
We are trying to help a diabetic get access to her blood sugar values so she can code new insulin dosing algorithms. (She wants to prevent the crashes and spikes in her blood sugars that ruin her days and put her at risk for blindness and being on dialysis. She believes and hopes human + machine can do better than her alone.)
If we are able to login the next steps for retrieving the values seem clear:
curl -k -X POST "https://share1.dexcom.com/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues?sessionID=GUID&minutes=1440&maxCount=1" -H "Accept: application/json" -H "Content-Length: 0"
Is there anything to try that could give us a clue as to how take the first step and login to this API?
Open the Dexcom clarity website
In your browser (let's assume Chrome for now) open the developer tools and go to the "Network" tab
Select the "data" XHR call. This is the "hidden API call" that populates the graphs in the Dexcom Clarity.
Right click and select "Copy > Copy as CURL (bash)". That will give you the CURL code that authenticates and logs into the website.
(Optional) Take the CURL command and paste it to https://curl.trillworks.com/ to translate the CURL code into some
other language
curl 'https://clarity.dexcom.com/api/subject/1522320180078ZZZZZZ/analysis_session/1560634749054XXXXXXX/data' -H 'Origin: https://clarity.dexcom.com'
-H 'Accept-Encoding: gzip, deflate, br'
-H ..... -H 'Referer: https://clarity.dexcom.com/' -H --data-binary '{"localDateTimeInterval":["2016-05-01/2016-07-29"]}' --compressed
Notice that you can change the data range by changing the values passed at localDateTimeInterval and get access to the full data in the account at a resolution of 5-min measurements. The returned JSON also includes all other events, such as calibrations, etc.

Understanding POST and JSON.parse in Ruby/Sinatra

I'm trying to understand the chain of events of how to POST a JSON object to a Sinatra API.
There will be a JSON object that will need to be passed through a POST request using Sinatra and here's my code that doesn't want to work:
namespace '/api/v1/registrations' do
post '/' do
my_hash = JSON.parse(request.body.read)
puts my_hash
end
end
Here is some test data I'm trying to post:
curl -H "Content-Type: application/json" -X POST http://localhost:4567/api/v1/registrations/ -d '{"i": "am json"}'
I get this error on the console:
JSON::ParserError - 757: unexpected token at '{i: am':
It seems that I would be able to POST a JSON object to /api/v1/registrations/ and I would see it parsed on the console so I can see it actually did something.
What is request.body.readreally doing? I've Googled my fingers off and I just need a laymans answer on how POSTing JSON works.

How to I find all data from JSON POST request with curl

I am playing with the open data project at spogo.co.uk (sport england).
See here for a search example: https://spogo.co.uk/search#all/Football Pitch/near-london/range-5.
I have been using cygwin and curl to POST JSON data to the MVC controller. An example is below:
curl -i -X POST -k -H "Accept: application/json" -H "Content-Type: application/json; charset=utf-8" https://spogo.co.uk/search/all.json --data '{"searchString":"","address": {"latitude":55,"longitude":-3},"page":0}'
Question:
How can I find out what other variables can be included in the post data?
How can I return all results, rather than just 20 at a time? Cycling through page numbers doesn't deliver all at once.
AJAX is simply a technique of posting data over a connection asynchronously, JSON is just a string format that can contain data. Neither of which have built in mechanisms for querying information such as what fields are accepted or the amount of data returned.
You will want to check the web service documentation for on spogo.co.uk for these answers, IF their web service exposes such functionality they will be the final authority on what the commands and formats are.

How to get xml post request parameters in Sinatra?

I have a very simple Sinatra app which only does log out the params in the action, and then I use curl to send post data in xml format, but sinatra didn't get the xml parsed:
echo '<something>tyrael tong</something>' | curl -X POST -H 'Content-Type: text/xml' -d #- http://localhost:9528/status/update
I searched through google with no solution to this. Am I suppose to parse the xml post data by myself?
try this code
require 'plist'
post 'some/route'
content_type :xml
value=Plist::parse_xml(request.body)
end
Find a rack parser: https://github.com/achiu/rack-parser which could do the job I want: parse the post body into parameter.
P.S. And need to set the content type to "application/xml"
Yes, content type is just a hint for a server how to handle it. If your server can receive say XML or JSON, content type can tell you how to parse it.
As the body of your post message is an xml it will not be parsed by either rails/sinatra. If you want parsing to be done you will need to change the format of data that you are sending and set the content type to "application/x-www-form-urlencoded". Then Sinatra will parse that data and put it into the params hash.

Resources