Rails postgreSQL API endpoint not available outside application, 400 error from PostMan

228 views Asked by At

Application is pretty simplistic. I am using the rails-api gem with postgres to generate JSON data, which is being called into the front end using Angular. I have taken CSRF protection off of the controller and set it to :null_session.

This is the basic crud functionality from the controller:

    class EngineersController < ApplicationController
    # GET /engineers
    # GET /engineers.json

    def index
      @engineers = Engineer.all

      render json: @engineers
    end

   # GET /engineers/1
   # GET /engineers/1.json

   def show
     @engineer = Engineer.find(params[:id])

     render json: @engineer
   end

  # POST /engineers
  # POST /engineers.json

  def create
     @engineer = Engineer.new(engineer_params)

    if @engineer.save
      render json: @engineer, status: :created, location: @engineer
    else
       render json: @engineer.errors, status: :unprocessable_entity
    end
  end

 # PATCH/PUT /engineers/1
 # PATCH/PUT /engineers/1.json

 def update
    @engineer = Engineer.find(params[:id])

   if @engineer.update(engineer_params)
      head :no_content
   else
     render json: @engineer.errors, status: :unprocessable_entity
   end
 end

 # DELETE /engineers/1
 # DELETE /engineers/1.json

 def destroy
   @engineer = Engineer.find(params[:id])
   @engineer.destroy

   head :no_content
 end

private

  def engineer_params
    params.require(:engineer).permit(:first_name, :last_name)
  end
end

This is just basic crud functionality defined with the rails api gem, and renders the views as bracketed JSON objects.

This is the application controller

class ApplicationController < ActionController::Base
 # Prevent CSRF attacks by raising an exception.
 # For APIs, you may want to use :null_session instead.

protect_from_forgery with: :null_session

  def index

  end
end

Index is defined to allow angular to render pages using ui-view, so it is the single actual view. The functionality works well, ng-repeat is used to loop through the json from postgres hitting the endpoint at localhost:8080/api/engineers and can read, add, and destroy content.

THE PROBLEM: The api is not accessible via postman, which means somehow it is not able to accept JSON data from outside the application. I would like to allow people to hit the api and update it from outside the application, or to simply understand why it is not working.

It is being sent with headers Content-Type application/json.

Relevent Server Logs

Error occurred while parsing request parameters.
Contents:

{first_name: "Samueeeel", last_name: "GRIMMMMES"}

ActionDispatch::ParamsParser::ParseError (795: unexpected token at '{first_name:
 "Samueeeel", last_name: "GRIMMMMES"}'):
0

There are 0 answers