Questions tagged [rails-api]

Rails for API only applications (>3.2)

Rails::API is a subset of a normal Rails application, created for applications that don't require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don't need the entire Rails middleware stack nor template generation.

Using Rails for API-only Apps

This is a quick walk-through to help you get up and running with Rails::API to create API-only Apps, covering:

  • What Rails::API provides for API-only applications
  • How to decide which middlewares you will want to include
  • How to decide which modules to use in your controller
753 questions
81
votes
2 answers

Is devise's token_authenticatable secure?

I'm building a simple api with Rails API, and want to make sure I'm on the right track here. I'm using devise to handle logins, and decided to go with Devise's token_authenticatable option, which generates an API key that you need to send with each…
Jeff Escalante
  • 3,137
  • 1
  • 21
  • 30
79
votes
4 answers

Rails API : Best way to implement authentication?

I'm writing a Rails 4 app that will expose an API for a mobile app that's yet to be developed. Users will authenticate using an e-mail and password from the mobile app. While I've found quite a bit of information on the topic. It's hard to discern…
Roma149
  • 1,401
  • 1
  • 12
  • 11
50
votes
3 answers

Controller Specs vs Request Specs?

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one…
jsanch16
  • 609
  • 1
  • 8
  • 9
30
votes
3 answers

How do you convert a Rails 5 API app to a rails app that can act as both API and app?

I initially created it in rails 5 with the --api tag. From http://edgeguides.rubyonrails.org/api_app.html, I removed config.api_only = true I changed class ApplicationController < ActionController::API end to class ApplicationController <…
Laser
  • 5,085
  • 4
  • 34
  • 48
30
votes
1 answer

How do you use authenticate_with_http_token with rails-api?

I am using rails-api and need to use authenticate_with_http_token to process a header authorization token, but i get the following error: NoMethodError (undefined method `authenticate_with_http_token' for ...)
andorov
  • 4,197
  • 3
  • 39
  • 52
30
votes
4 answers

Rails: Undefined method `flash' for ActionDispatch::Request

I'm trying to write an Ember application in Rails 4, and have decided to go with rails-api for the api controllers, while keeping the application controller intact for a few pages that aren't part of the single-page app. To put it in more concrete…
Jonathan Bender
  • 1,911
  • 4
  • 22
  • 39
25
votes
3 answers

Rails: Restrict API requests to JSON format

I would like to restrict requests to all API controllers to being redirected to the JSON path. I would like to use a redirect since also the URL should change according to the response. One option would be to use a before_filter which redirects the…
JJD
  • 50,076
  • 60
  • 203
  • 339
23
votes
2 answers

Rails JSON API oauth authentication

I've been researching the best approach for implementing facebook, twitter, and email/password authentication for a pure JSON rails 4.1 application that uses rails-api. The rails app has NO views and just serves JSON between android and iphone…
John
  • 4,362
  • 5
  • 32
  • 50
22
votes
4 answers

devise user sign_in gives authentication error for CSRF token authenticity token

I am using devise (latest version - 3.2.0) with rails (latest version - 4.0.1) I'm doing simple authentication (without ajax or api) and getting an error for CSRF authenticity token. Check the POST request below started POST "/users/sign_in" for…
rtcoms
  • 783
  • 1
  • 8
  • 19
19
votes
2 answers

How to get a downloadable file from a readableStream response in a fetch request

I have a react app that sends a POST request to my rails API. I want my API endpoint to generate a csv file and then to send back that file to the react app. I want then the browser to download of the csv file for the end user. Here's how the…
David Geismar
  • 3,152
  • 6
  • 41
  • 80
17
votes
4 answers

Token based authentication for Rails JSON APIs

I make API in rails. For normal authentication we use devise but in API how to implement devise for authentication. gem 'devise_token_auth' Someone prefer this this gem for authentication but there are no tutorial available for that. How to…
user5496955
17
votes
2 answers

authenticate_or_request_with_http_token returning html instead of json

I've created a rails-api application and proceeded to secure it using token authentication. I've set a before_filter that is calling a method which uses authenticate_or_request_with_http_token. Everything is working fine but, when the authentication…
Federico Nafria
  • 1,397
  • 14
  • 39
17
votes
2 answers

rails-api authentication by header's token

I'd like to work with rails-api gem special to create API-only application. To provide authentication mechanism I want to use built-in authenticate_or_request_with_http_token method described in Railscasts #352, but this method in missing…
Anatoly
  • 15,298
  • 5
  • 53
  • 77
16
votes
2 answers

How to rescue ActionDispatch::ParamsParser::ParseError and return custom API error in rails 5?

Whenever sending malformed JSON against my API-only Rails 5.x application, I get an exception and Rails is returning the entire stack trace as JSON. Obviously I'd like to respond with a nicely, custom, formatted error. => Booting Puma => Rails…
nakwa
  • 1,157
  • 1
  • 13
  • 25
16
votes
4 answers

Display or get the HTTP header attributes in Rails 4

I have an application developed in Rails and I am trying to see the attributes in the HTTP header. Is there any way to read these attributes? Where are they stored? Someone mentioned request.headers. Is this correct? I am not able to see any…
delpha
  • 931
  • 1
  • 8
  • 23
1
2 3
50 51