如何将Httparty与Rails 6结合使用

时间:2020-06-29 11:22:42

标签: ruby-on-rails ruby

我正在使用Rails应用程序向服务器发送发布消息,但是每次我尝试向服务器发送发布请求时,似乎都没有发送到服务器的发布数据。但是,如果我使用邮递员以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

1 个答案:

答案 0 :(得分:1)

我认为您必须像下面那样更改语法:-

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
           )