Reputation: 69
I created a body for a PUT request with Ruby. When I print out the body, I don't see any problems. But when I try to print out the body from the actual PUT request, I start getting an error.
To elaborate, here is my code:
@data={param1: "nameserver",
param2: {code: "XYZ", name: "NAME", start: "2017"}}
puts "data = #{@data} " #This works fine
@putResponse = HTTParty.put(base_url,
:body => @data.to_json,
:headers => header)
puts "putResponse.body is #{@putResponse.body}" #This is where I get the error
So as you can see, the line puts "data = #{@data} "
works fine. It prints out
data = {:param1=>"nameserver", :param2=>{:code=>"XYZ", :name=>"NAME", :start=>"2017"}}
But the line puts puts "putResponse.body is #{@putResponse.body}"
doesn't work. This is what it prints out:
putResponse.body is {"errors":[{"message":"The specified resource does not exist."}],"error_report_id":443}
So what's the problem here? (I'm using HTTParty to make my PUT request)
EDIT:
Here's how I got the host and header:
config = JSON.parse(File.read('config.json'))
puts "config: #{config}"
access_token = config['canvas']['access_token']
puts "access_token: #{access_token}"
host = config['canvas']['host']
puts "host: #{host}"
base_url = 'http://#{host}/api/v1/users/self/custom_data/program_of_study'
puts "base_url: #{base_url}"
header = {'Authorization': 'Bearer ' "#{$access_token}", "Content-Type" => 'application/json', 'Accept' => 'application/json'}
Upvotes: 0
Views: 1685
Reputation: 14776
Dealing with PUT
in Sinatra, is similar to dealing with POST
- this is why the documentation may be scarce in this aspect.
This is a simple example of a Sinatra endpoint that receives PUT
arguments from a curl
request:
# server.rb
require 'sinatra'
put '/' do
params.inspect
end
And test it with this curl command:
$ curl -X PUT -d n=hello http://localhost:4567/
The params
hash will be available to you inside any Sinatra endpoint, including parameters received by any HTTP method.
In response to your comment, it is hard to debug without seeing your entire code. I suggest you run the tests provided in this answer, and gradually modify it to fit your actual code, to understand what breaks.
With the above server.rb
running, the below test works without errors:
# test.rb
require 'httparty'
data = {
param1: "nameserver",
param2: { code: "XYZ", name: "NAME", start: "2017" }
}
response = HTTParty.put 'http://localhost:4567/', body: data
puts response.body
# => {"param1"=>"nameserver", "param2"=>{"code"=>"XYZ", "name"=>"NAME", "start"=>"2017"}}
Upvotes: 1