0

I have this curl command which I need to covert to PUT request

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

Trial

I tried this PUT, but it doesn't work. It doesn't throw any error, but it does not add the description.

put(
     "https://example.com/api/v2/students/id.json",
     {:student => {:description  => {:body => 'Adding a new test description.'}}},
     { 'Authorization' => "Basic #{authorization_token}" }
     )
Noxx
  • 354
  • 1
  • 19
Ashley
  • 147
  • 2
  • 20

2 Answers2

0
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

use

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}
)
Sully
  • 14,672
  • 5
  • 54
  • 79
0

In your curl example, you provided the body as a (JSON-formatted) string:

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

The direct equivalent in rest-client would also use a (JSON-formatted) string:

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

According to the README:

rest-client does not speak JSON natively, so serialize your payload to a string before passing it to rest-client.


You can use the rest-client log to show the actual HTTP request sent, and compare it with what curl sends.

RJHunter
  • 2,829
  • 3
  • 25
  • 30
  • What was the difference in the HTTP request (as reported by the rest-client log and the curl trace)? – RJHunter Mar 16 '19 at 23:03