How to accept post request via json on rails? - ruby-on-rails

I have a controller which accepts both html and json format data. I want to know how to accept the data of json format from the POST request from the browser's REST client. Null values are getting stored in the database. How to convert Json format data into string to store it in the database? I have used rabl gem to filter json contents. Ruby on Rails.

You might want to take a look at the serialize class method:
http://apidock.com/rails/ActiveRecord/Base/serialize/class
This allows one to easily save the incoming JSON format and parse it to a Hash when retreiving it from the database.

Related

On a swagger-UI page, can I default the json to explore with a query string parameter?

I have a swagger-ui/index.html I would like to be able to publish URLs to various APIs using the same html. Is here a way to pass in the json to be explored via a query string parameter?
Yes, this can be done by passing in the json path using ../swagger-ui/?url=http://something.com/swagger.json

Using JSON with Michael hartl tutorial?

I would like to save a JSON file in the JavaScript folder and read data from it and save the data to the database. How do I read a JSON file in Ruby on Rails 4?
You should do a respond_to block in your controller, return JSON data and parse it with JavaScript in your view.
If you need to view the straight JSON data just add ".json" (www.your_url.com.json) to the end of the url in your browser and you'll see the data returned, in which case you can copy it and save it wherever you like.

Form posting JSON data which includes a url is being split at the query string of the url

I need to post JSON data to an MVC controller that contains URL's. The JSON data looks like it's being split at the query string (=)
The JSON data looks like this:
"{"Files":[{"Title":"test","OriginalFileName":"",
"FileName":"http://company.domain.com/auth.aspx?enrollmentkey=APK54cd1546a8454d4ca79ded89a78f8698",
"Categories":[{"CategoryId":76,"SubCategoryId":182,"CatId":"CatId0"}],
"TypeId":"84",
"Tags":["Select Tag(s)..."],
"TagIds":[],
"Roles":[],
"MemberOnly":false,
"ContentTypeId":7,
"Id":0,
"IsPublished":true,
"PublishDate":""}]}"
Debugging, I see that it's being split into
KEY (Request.Form.GetKey(0)):
{"Files":[{"Title":"Test","OriginalFileName":"","FileName":"http://company.domain.com/auth.aspx?enrollmentkey
VALUE (Request.Form.GetValue(0)):
APK54cd1546a8454d4ca79ded89a78f8698","Categories":[{"CategoryId":110,"SubCategoryId":111,"CatId":"CatId0"}],"TypeId":"69","Tags":["Select Tag(s)..."],"TagIds":[],"Roles":[],"MemberOnly":false,"ContentTypeId":7,"Id":0,"IsPublished":true,"PublishDate":""}]}
Does the JSON data needs to be escaped at the = or the whole thing needs to be encoded or am I missing something?
I should note that I'm using knockout's ko.toJSON(js) to create the JSON although I'm not sure that is relevant.
I also noticed that chrome dev tools also seems to recognize the Key-Val split:
If you are sending JSON data to the server, the Content-Type header needs to be set to application/json. If it is set to application/x-www-form-urlencoded then the server will try to interpret the JSON as key-value pairs as in a URL. This is why your JSON string is getting broken in two at the =.

Parsing twitter json response

I've made a twitter api request (GET trends/place API) and the result is just like this:
[{"trends":[{"name":"#BabyIsStaying","url":"http:\/\/twitter.com\/search?q=%23BabyIsStaying","promoted_content":null,"query":"%23BabyIsStaying","events":null},{"name":"#AkTakipBa\u015fl\u0131yor","url":"http:\/\/twitter.com\/search?q=%23AkTakipBa%C5%9Fl%C4%B1yor","promoted_content":null,"query":"%23AkTakipBa%C5%9Fl%C4%B1yor","events":null},{"name":"#\u00c7apulcuTakip","url":"http:\/\/twitter.com\/search?q=%23%C3%87apulcuTakip","promoted_content":null,"query":"%23%C3%87apulcuTakip","events":null},{"name":"#SadeceKar\u015f\u0131l\u0131kl\u0131Takiple\u015fme","url":"http:\/\/twitter.com\/search?q=%23SadeceKar%C5%9F%C4%B1l%C4%B1kl%C4%B1Takiple%C5%9Fme","promoted_content":null,"query":"%23SadeceKar%C5%9F%C4%B1l%C4%B1kl%C4%B1Takiple%C5%9Fme","events":null},{"name":"#soysuzek\u015fis\u00f6zl\u00fck","url":"http:\/\/twitter.com\/search?q=%23soysuzek%C5%9Fis%C3%B6zl%C3%BCk","promoted_content":null,"query":"%23soysuzek%C5%9Fis%C3%B6zl%C3%BCk","events":null},{"name":"B\u00fcy\u00fck FENERBAH\u00c7E Taraftarlar\u0131Takiple\u015fiyor","url":"http:\/\/twitter.com\/search?q=%22B%C3%BCy%C3%BCk+FENERBAH%C3%87E+Taraftarlar%C4%B1Takiple%C5%9Fiyor%22","promoted_content":null,"query":"%22B%C3%BCy%C3%BCk+FENERBAH%C3%87E+Taraftarlar%C4%B1Takiple%C5%9Fiyor%22","events":null},{"name":"Sar\u0131K\u0131rm\u0131z\u0131Aile UnfollowsuzTakiple\u015fiyor","url":"http:\/\/twitter.com\/search?q=%22Sar%C4%B1K%C4%B1rm%C4%B1z%C4%B1Aile+UnfollowsuzTakiple%C5%9Fiyor%22","promoted_content":null,"query":"%22Sar%C4%B1K%C4%B1rm%C4%B1z%C4%B1Aile+UnfollowsuzTakiple%C5%9Fiyor%22","events":null},{"name":"D\u00fcnyadaTeksinSen GALATASARAY\u0131m","url":"http:\/\/twitter.com\/search?q=%22D%C3%BCnyadaTeksinSen+GALATASARAY%C4%B1m%22","promoted_content":null,"query":"%22D%C3%BCnyadaTeksinSen+GALATASARAY%C4%B1m%22","events":null},{"name":"D\u00fcnyan\u0131n TekHarikas\u0131s\u0131n FENERBAH\u00c7EM","url":"http:\/\/twitter.com\/search?q=%22D%C3%BCnyan%C4%B1n+TekHarikas%C4%B1s%C4%B1n+FENERBAH%C3%87EM%22","promoted_content":null,"query":"%22D%C3%BCnyan%C4%B1n+TekHarikas%C4%B1s%C4%B1n+FENERBAH%C3%87EM%22","events":null},{"name":"MustafaKemalin\u0130zindeyiz \u00c7\u00fcnk\u00fcFenerbah\u00e7eliyiz","url":"http:\/\/twitter.com\/search?q=%22MustafaKemalin%C4%B0zindeyiz+%C3%87%C3%BCnk%C3%BCFenerbah%C3%A7eliyiz%22","promoted_content":null,"query":"%22MustafaKemalin%C4%B0zindeyiz+%C3%87%C3%BCnk%C3%BCFenerbah%C3%A7eliyiz%22","events":null}],"as_of":"2013-07-20T10:51:45Z","created_at":"2013-07-20T10:45:24Z","locations":[{"name":"Turkey","woeid":23424969}]}]
My question is how to turn this array string into html. No proper php experience at all.
The response you are getting is JSON.
You used to be able to get response in XML, RSS and more, but now the only response type you will receive is JSON.
PHP has multiple methods for dealing with JSON. To encode data, you would use json_encode(). However, you want to decode it, so you want to be doing the following:
var_dump(json_decode($yourData));
This will dump the data to the browser so you can see what you have to work with.
You can iterate around this data using a foreach() loop.
Remember, assuming you json_decode() your results into $yourData:
To access array properties, use: $yourData['propertyName']
To access object properties, use: $yourData->propertyName
you can feed that into json_decode() function
you will get an array that you can manage as you want.

Caching/storing twitter API data in MySQL db

I have a database of twitter usernames in a mysql DB. I would like to populate the currently empty description, location and url fields with data.
What would be be the best way to do this?
Thanks!
You should take a look at the Twitter REST API documentation. There should be something in there you can use.
For example, if you use the following url, you get information in JSON format on a particular user: http://twitter.com/users/show/<userid>.json. If you end your url with .xml instead of .json, you get the same info in xml format.
Then in your application you can parse the returned data and add the missing values to your database.

Resources