Geni
Geni

Reputation: 25

How can I use Httparty with rails 6

I am working on a rails application to send a post message to a server but each time i tried to send a post request to the sever there seems to be no post data sent to the server. However, if I use postman to send the message with json format the server responds correctly to the post request. I am relatively new to rails and i am not sure if my configuration is wrong. Please I need help. Here is my my code:

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

Upvotes: 0

Views: 695

Answers (1)

code_aks
code_aks

Reputation: 2074

I think you have to change your syntax like below :-

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
       )  

#syntax

response = HTTParty.post(url,
            headers: {},
            body: {}.to_json
           )  

Upvotes: 1

Related Questions