构建POST请求和返回JSON数据 [英] Building POST Request and Return JSON Data

查看:308
本文介绍了构建POST请求和返回JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法建立一个POST请求,该请求应接受个人电话号码的CSV,即。 phoneNum1,phoneNum2,phoneNum3 ... phoneNum350

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

,然后返回与之匹配的电话号码的JSON对象在数据库中。我使用Chrome扩展程序POSTMAN测试:

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:

,但执行后它返回 ERROR 404

如何重新格式化此请求以工作?

How can I reformulate this request to work?

操作:

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

路线:

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


推荐答案

问题是你的路线:

/getActivatedFriends/:friends_phone_number_csv

p>

Should be:

/getActivatedFriends

:friends_phone_number_csv表示您正在传递网址参数。

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

这篇关于构建POST请求和返回JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆