我正在嘗試設定 Faraday 以向 Twilio API 發出請求。我可以通過 Postman 將請求正文中的鍵/值設定為x-www-form-urlencoded資料來發出請求。
當我嘗試在 Rails 中復制我在 Postman 上創建的 cURL 時,我收到一個錯誤,好像我在有效負載中發送的鍵/值對無法識別
以下 cURL 請求適用于 Postman:
curl --location --request POST 'https://api.twilio.com/2010-04-01/Accounts/TOKEN1234/Messages.json' \
--header 'Authorization: Basic AUTH_TOKEN==' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'From=whatsapp: 5491112312312' \
--data-urlencode 'Body=Hello. Your order is on the way' \
--data-urlencode 'To=whatsapp: 541132132121'
我的法拉第連接器如下所示:
class Twilio::SubAccountConnector
attr_reader :sid, :auth_token, :phone, :api_url
def initialize(account)
@sid = account.twilio_configuration.sid
@auth_token = account.twilio_configuration.auth_token
@phone = account.twilio_configuration.phone
@api_url = "https://api.twilio.com/2010-04-01/Accounts/#{sid}/Messages.json"
end
def form_data
{
from: "whatsapp: 5491112312312",
body: "Hello. Your order is on the way",
to: "whatsapp: 541132132121",
}
end
def send_whatsapp_notification
connector.post do |req|
req.body = form_data
end
end
private
def connector(url = api_url)
Faraday.new(url: url) do |faraday|
faraday.request :basic_auth, sid, auth_token
faraday.request :url_encoded
faraday.response :json
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
end
這是法拉第請求中的請求正文:
request_body=
"{\"From\":\"whatsapp: 5491112312312\",\"Body\":\"Hello. Your order is on the way\",\"To\":\"whatsapp: 541132132121\"}"
我在回應正文中收到以下錯誤,所以我想我在發送有效負載的方式上做錯了,因為無法識別鍵/值對。
response_body={"code"=>21604, "message"=>"A 'To' phone number is required.", "more_info"=>"https://www.twilio.com/docs/errors/21604", "status"=>400}>
我是否在連接器方法中遺漏了某些內容,因此有效負載被正確編碼?
uj5u.com熱心網友回復:
問題是引數應該以大寫字母開頭。您的法拉第請求在其他方面是正確的,但您的form_data方法應如下所示:
def form_data
{
From: "whatsapp: 5491112312312",
Body: "Hello. Your order is on the way",
To: "whatsapp: 541132132121",
}
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490088.html
上一篇:JSON::ParserErrorin...“../../test.json”處的意外令牌-RUBYONRAILS
下一篇:Xamarin表單中的影像輸入
