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 [email protected]: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}" }
     )
7
  • Welcome to stackoverflow. Snippets are for frontend JS/HTML/CSS problems. Other code should just be indented by four spaces (select the text and press the {} button on the edit window).
    – max
    Mar 16, 2019 at 20:55
  • “Doesn’t work” means exactly what? Does it print an error message? Mar 16, 2019 at 21:14
  • It does not throw any error message but is not adding the description that is given. It adds the description with the curl command
    – Ashley
    Mar 16, 2019 at 21:16
  • Try {student: {description: {body: 'Adding a new test description.'}}}
    – Sully
    Mar 16, 2019 at 21:27
  • Tried but not working. No error but not adding the description either
    – Ashley
    Mar 16, 2019 at 21:30

2 Answers 2

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.

1
  • What was the difference in the HTTP request (as reported by the rest-client log and the curl trace)?
    – RJHunter
    Mar 16, 2019 at 23:03
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 [email protected]:Abcd1234 \
  -X PUT

use

put(
     "https://test%40gmail.com:[email protected]/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}
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.