[英]How can I use Httparty with rails 6
我正在開發一個 Rails 應用程序以向服務器發送發布消息,但每次我嘗試向服務器發送發布請求時,似乎都沒有向服務器發送發布數據。 但是,如果我使用 postman 以 json 格式發送消息,則服務器會正確響應發布請求。 我對 Rails 比較陌生,我不確定我的配置是否錯誤。 請我需要幫助。 這是我的代碼:
def send_information
HTTParty.post("https://example.com/api/json.php",
:body => {
user: 'Nancy Cole',
item: 'mobile phone',
content: 'Hurray the server has received your message',
id: 2,
:headers => {
'Content-Type' => 'application/json' }
}
)
end
我認為你必須改變你的語法如下: -
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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.