0

我尝试了以下方法: Ruby:如何使用 Curb 发送 JSON POST 请求?

但我得到一个错误,但curl工作方式如下:

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
}' 

但在遏制中,这失败了:

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

当我查看 request.bin 帖子时,它与 curl 不同。这是来自Curb

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

这是来自终端卷曲:

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

4

1 回答 1

0

确保 url 是一个字符串,payload 是 JSON。这应该有效。

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
于 2015-08-24T15:38:22.957 回答