rubylinodefaraday

How to send json using faraday


I need to consume the linode api, in the description it says I have to send the data using curl like this

curl -H "Content-Type: application/json" \
    -H "Authorization: Bearer $TOKEN" \
    -X POST -d '{
        "label": "asdfasdf"
        "ssh_key": "asdfasdf"
    }' \
    https://api.linode.com/v4/profile/sshkeys

I tried using


http = Faraday::Connection.new 'https://api.linode.com/v4/profile/sshkeys'
http.authorization :Bearer, token
http.post('', { 'label' => 'adfadf', ..}.to_json)

But every request it says label and ssh_key required. I don´t know how to send this particular request

anyone?


Solution

  • You need to specify the content-type as json in the header, if you're sending a json data.

    http.post(
      '',
      { 'label' => 'adfadf', ..}.to_json,
      { 'Content-Type' => 'application/json' }
    )
    

    Reference: https://lostisland.github.io/faraday/usage/