【问题标题】:How to send json using faraday如何使用法拉第发送 json
【发布时间】:2021-10-06 20:29:42
【问题描述】:

我需要使用 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)

但是每个请求都说需要 label 和 ssh_key。我不知道如何发送这个特定的请求

有人吗?

【问题讨论】:

    标签: ruby linode faraday


    【解决方案1】:

    如果您要发送 json 数据,则需要在标头中将 content-type 指定为 json。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多