how do I POST several values + headers using curb

684 Views Asked by At

I tried the following: Ruby: How do I send a JSON POST request using Curb?

But I get an error, yet curl works as follows:

curl "https://api-sandbox.billforward.net:443/v1/subscriptions/SUB-35F07614-4F6D-4AF2-95EB-F3DB03FE/advance" \
    -H "Authorization: Bearer token-goes-here" \
    -H "Content-Type: application/json" \
    -d \
'{
  "periods": 1,
  "skipIntermediatePeriods": false,
  "handleAmendments": true
}' 

But in curb, this fails:

url = "https://api-sandbox.billforward.net:443/v1/subscriptions/#{subscription_id}}/advance"
    #token = 'b3b9dfb6-ea2a-48c1-afd3-96e41a142987'

    payload = {:periods => periods,
                :skipIntermediatePeriods => false,
                :handleAmendments => false}

    p payload
    payload_json = payload.to_json
curl_response = Curl.post(url, payload_json) do |curl|
                          curl.headers['Accept'] = 'application/json'
                          curl.headers['Content-Type'] = 'application/json'
                          curl.headers['Authorization'] = "Bearer #{@token}"
                          curl.verbose=true
                        end

When I look at the request.bin post, it's different from curl. This is from Curb:

{"periods":1,"skipIntermediatePeriods":false,"handleAmendments":true}

This is from terminal curl:

{ "to": "2015-11-22T19:26:15Z", "skipIntermediatePeriods": true, "handleAmendments": false }

1

There are 1 best solutions below

3
On

Make sure the url is a string and the payload is JSON. This should work.

Curl.post(URI.join(BASE_URL, "example.json").to_s, payload.to_json) do |curl|
  curl.headers['Accept'] = 'application/json'
  curl.headers['Content-Type'] = 'application/json'
  curl.headers['Authorization'] = "Bearer #{token}"
  curl.verbose=true
end