Haseeb Ahmad
Haseeb Ahmad

Reputation: 8730

How to send header parameters from swagger

For testing rails api I am using Swagger.

module Api
class UserController < ApplicationController
    Swagger::Docs::Generator::set_real_methods
    skip_before_filter :verify_authenticity_token

    # start swagger actions
    swagger_controller :users, "Users"
    swagger_api :create do
        summary "SignUp"
        param :form, :first_name, :string, :optional, "First Name"
        param :form, :last_name, :string, :optional, "Last Name"
        param :form, :phone_no, :string, :required, "Phone Number"
        param :form, :email, :string, :optional, "Email"
        param :form, :password, :string, :required, "Password"
        param :form, :password_confirmation, :string, :required, "Confirm Password"
    end

I am using Authentication Via an Access Token. But how to send headers from swagger?

Upvotes: 3

Views: 3267

Answers (4)

RohitPorwal
RohitPorwal

Reputation: 1065

Rakesh's answer is right. For simplified, use this in swagger_api block:

param :header, 'SessionToken', :string, :required, "SessionToken"

Upvotes: 2

rakesh kashyap
rakesh kashyap

Reputation: 1466

can you please try to add the below and check if this works

param :header, 'Authentication-Token', :string, :required, 'Authentication token'

If you feel it is a pain to add this on all the controllers you can do something like

def setup_basic_api_documentation
      [:controller1, :controller2, :controller3].each do |api_action|
        swagger_api api_action do
          param :header, 'Authentication-Token', :string, :required, 'Authentication token'
end

Upvotes: 1

random-forest-cat
random-forest-cat

Reputation: 35914

I am using swagger blocks with Sinatra, so the syntax may be a little bit different, but this worked for me.

Use key :in, :header like this:

  parameter do
    key :name, 'X-APPLICATION-ID'
    key :in, :header
    key :description, 'client name'
    key :required, false
    key :type, :string
  end

Visit open api specification for more on the parameter object

Upvotes: 1

Pardeep Saini
Pardeep Saini

Reputation: 2102

we can set paramtype header for a field on the api delcaration and then the field gets populated on the swagger ui and gets sent as header in the call.

Upvotes: 1

Related Questions