Building POST Request and Return JSON Data

2019-09-01 15:16发布

问题:

I am having trouble building a POST request that should accept a CSV of a persons phone numbers ie. phoneNum1,phoneNum2,phoneNum3...phoneNum350

and then return a JSON object of those phone numbers that matched already in the database. I am testing with the chrome extension POSTMAN as such:

yet it returns ERROR 404 upon execution.

How can I reformulate this request to work?

the action:

def getActivatedFriends
    @results = BusinessUser.find_by_sql("SELECT 
                                            a.id
                                         ,  a.username
                                         ,  a.phoneNumber
                                         FROM sers a
                                         WHERE phoneNumber in (" + params[:friends_phone_number_csv].to_s + ") and
                                               removed = 0 and
                                               is_user = 1;")

    respond_to do |format|
        format.html
        format.json { render json: { friends_match: @results }}
    end         
end

the route:

  match '/getActivatedFriends/:friends_phone_number_csv',
  to: 'requests#getActivatedFriends', via: 'post',
  constraints: { friends_phone_number_csv: /([0-9]+,?)+/ } 

回答1:

The problem is with your route:

/getActivatedFriends/:friends_phone_number_csv

Should be:

/getActivatedFriends

The :friends_phone_number_csv implies that you are passing a URL parameter.