Ruby on rails 如何将Httparty与rails 6结合使用
我正在使用rails应用程序向服务器发送post消息,但每次尝试向服务器发送post请求时,似乎都没有向服务器发送post数据。但是,如果我使用postman以json格式发送消息,服务器会正确响应post请求。我对rails比较陌生,我不确定我的配置是否错误。求你了,我需要帮助。这是我的代码:Ruby on rails 如何将Httparty与rails 6结合使用,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用rails应用程序向服务器发送post消息,但每次尝试向服务器发送post请求时,似乎都没有向服务器发送post数据。但是,如果我使用postman以json格式发送消息,服务器会正确响应post请求。我对rails比较陌生,我不确定我的配置是否错误。求你了,我需要帮助。这是我的代码: def发送信息 HTTParty.post(“https://example.com/api/json.php", :body=>{ 用户:“南希·科尔”, 项目:'移动电话', 内容:“万岁!服务器已
def发送信息
HTTParty.post(“https://example.com/api/json.php",
:body=>{
用户:“南希·科尔”,
项目:'移动电话',
内容:“万岁!服务器已收到您的消息”,
id:2,
:headers=>{
'内容类型'=>'应用程序/json'}
}
)
结束
我认为您必须更改语法,如下所示:-
response = HTTParty.post("https://example.com/api/json.php",
headers: {
"Content-Type" => "application/json"
},
body: {user: 'Nancy Cole',
item: 'mobile phone',
content: 'Hurray the server has received your message',
id: 2
}.to_json
)
#语法
response = HTTParty.post(url,
headers: {},
body: {}.to_json
)