【问题标题】:How do I convert this curl command to Ruby rest-client put request?如何将此 curl 命令转换为 Ruby rest-client put 请求?
【发布时间】:2019-08-07 15:20:42
【问题描述】:

我有这个 curl 命令,我需要将其转换为 PUT 请求

curl https://example.com/api/v2/students/id.json \
  -d '{"student":{"description":{"body":"Adding a new test description"}}}' \
  -H "Content-Type: application/json" \
  -v -u test@gmail.com:Abcd1234 \
  -X PUT

试用

我尝试了这个 PUT,但它不起作用。它不会抛出任何错误,但不会添加描述。

put(
     "https://example.com/api/v2/students/id.json",
     {:student => {:description  => {:body => 'Adding a new test description.'}}},
     { 'Authorization' => "Basic #{authorization_token}" }
     )

【问题讨论】:

  • 欢迎来到stackoverflow。片段用于前端 JS/HTML/CSS 问题。其他代码应该只缩进四个空格(选择文本并按下编辑窗口上的{} 按钮)。
  • “不起作用”到底是什么意思?它会打印错误消息吗?
  • 它不会抛出任何错误消息,但不会添加给出的描述。它使用 curl 命令添加描述
  • 试试{student: {description: {body: 'Adding a new test description.'}}}
  • 试过但没用。没有错误,但也没有添加描述

标签: ruby-on-rails ruby api rest-client


【解决方案1】:

在您的 curl 示例中,您将正文作为(JSON 格式)字符串提供:

curl ... \
  -d '{"student":{"description":{"body":"Adding a new test description"}}}' \
  ...

rest-client 中的直接等价物也将使用(JSON 格式)字符串:

put( ...,
  '{"student":{"description":{"body":"Adding a new test description"}}}',
  ...
)

根据README

rest-client 本身不使用 JSON,因此在将负载传递给 rest-client 之前将其序列化为字符串。


您可以使用 rest-client 日志显示实际发送的 HTTP 请求,并将其与 curl 发送的内容进行比较。

【讨论】:

  • HTTP 请求有什么区别(由 rest-client 日志和 curl 跟踪报告)?
【解决方案2】:
curl https://example.com/api/v2/students/id.json \
  -d '{"student":{"description":{"body":"Adding a new test description"}}}' \
  -H "Content-Type: application/json" \
  -v -u test@gmail.com:Abcd1234 \
  -X PUT

使用

put(
     "https://test%40gmail.com:Abcd1234@example.com/api/v2/students/id.json",
     {student: {description: {body: 'Adding a new test description.'}}},
     #{'student': {'description': {'body': 'Adding a new test description.'}}},
     #{:student => {:description  => {:body => 'Adding a new test description.'}}}.to_json,
     {content_type: :json, accept: :json}
)

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2020-04-29
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    相关资源
    最近更新 更多