微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用法拉第发送json

如何解决如何使用法拉第发送json

我需要使用 linode api,在描述中它说我必须像这样使用 curl 发送数据

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

我尝试使用


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

但是它说每个请求都需要标签和 ssh_key。我不知道如何发送此特定请求

有人吗?

解决方法

如果您要发送 json 数据,则需要在标头中将内容类型指定为 json。

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

参考:https://lostisland.github.io/faraday/usage/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。