如何格式化 HTTParty POST 请求?
How to format HTTParty POST request?
我一直在尝试 API 对我正在进行的项目的调用,当我尝试将一些 JSON 传递给 POST 要求。该调用在 Postman 中有效,但我不知道如何在 Ruby 中格式化它。这是我的代码:
require 'httparty'
require 'json'
require 'pp'
#use the HTTParty gem
include HTTParty
#base_uri 'https://app.api.com'
#set some basic things to make the call,
@apiUrl = "https://app.api.com/"
@apiUrlEnd = 'apikey=dontStealMePls'
@apiAll = "#{@apiUrl}#{@apiUrlEnd}"
@apiTest = "https://example.com"
def cc_query
HTTParty.post(@apiAll.to_s, :body => {
"header": {"ver": 1,"src_sys_type": 2,"src_sys_name": "Test","api_version": "V999"},
"command1": {"cmd": "cc_query","ref": "test123","uid": "abc01", "dsn": "abcdb612","acct_id": 7777}
})
end
def api_test
HTTParty.post(@apiTest.to_s)
end
#pp api_test()
pp cc_query()
这段代码给我这个错误:
{"fault"=>
{"faultstring"=>"Failed to execute the ExtractVariables: Extract-Variables",
"detail"=>{"errorcode"=>"steps.extractvariables.ExecutionFailed"}}}
我知道这个错误,因为如果我尝试在呼叫正文中没有任何 JSON 的情况下(通过 Postman)进行呼叫,我会得到它。因此,我假设我上面的代码在进行 API 调用时没有传递任何 JSON 。我的 JSON 格式不正确吗?我是否正确格式化了 .post 调用?任何帮助表示赞赏! :)
api_test() 方法只是对 example.com 进行了 POSt 调用,它起作用了(拯救了我的理智)。
只需在 class 中使用 HTTParty 作为混入即可:
require 'httparty'
class MyApiClient
include HTTParty
base_uri 'https://app.api.com'
format :json
attr_accessor :api_key
def initalize(api_key:, **options)
@api_key = api_key
@options = options
end
def cc_query
self.class.post('/',
body: {
header: {
ver: 1,
src_sys_type: 2,
src_sys_name: 'Test',
api_version: 'V999'
},
command1: {
cmd: 'cc_query',
ref: 'test123',
uid: 'abc01',
dsn: 'abcdb612',
acct_id: 7777
}
},
query: {
api_key: api_key
}
)
end
end
用法示例:
MyApiClient.new(api_key: 'xxxxxxxx').cc_query
当你使用format :json
时,HTTParty会自动设置内容类型并处理JSON编码和解码。我猜这就是你失败的地方。
我一直在尝试 API 对我正在进行的项目的调用,当我尝试将一些 JSON 传递给 POST 要求。该调用在 Postman 中有效,但我不知道如何在 Ruby 中格式化它。这是我的代码:
require 'httparty'
require 'json'
require 'pp'
#use the HTTParty gem
include HTTParty
#base_uri 'https://app.api.com'
#set some basic things to make the call,
@apiUrl = "https://app.api.com/"
@apiUrlEnd = 'apikey=dontStealMePls'
@apiAll = "#{@apiUrl}#{@apiUrlEnd}"
@apiTest = "https://example.com"
def cc_query
HTTParty.post(@apiAll.to_s, :body => {
"header": {"ver": 1,"src_sys_type": 2,"src_sys_name": "Test","api_version": "V999"},
"command1": {"cmd": "cc_query","ref": "test123","uid": "abc01", "dsn": "abcdb612","acct_id": 7777}
})
end
def api_test
HTTParty.post(@apiTest.to_s)
end
#pp api_test()
pp cc_query()
这段代码给我这个错误:
{"fault"=>
{"faultstring"=>"Failed to execute the ExtractVariables: Extract-Variables",
"detail"=>{"errorcode"=>"steps.extractvariables.ExecutionFailed"}}}
我知道这个错误,因为如果我尝试在呼叫正文中没有任何 JSON 的情况下(通过 Postman)进行呼叫,我会得到它。因此,我假设我上面的代码在进行 API 调用时没有传递任何 JSON 。我的 JSON 格式不正确吗?我是否正确格式化了 .post 调用?任何帮助表示赞赏! :)
api_test() 方法只是对 example.com 进行了 POSt 调用,它起作用了(拯救了我的理智)。
只需在 class 中使用 HTTParty 作为混入即可:
require 'httparty'
class MyApiClient
include HTTParty
base_uri 'https://app.api.com'
format :json
attr_accessor :api_key
def initalize(api_key:, **options)
@api_key = api_key
@options = options
end
def cc_query
self.class.post('/',
body: {
header: {
ver: 1,
src_sys_type: 2,
src_sys_name: 'Test',
api_version: 'V999'
},
command1: {
cmd: 'cc_query',
ref: 'test123',
uid: 'abc01',
dsn: 'abcdb612',
acct_id: 7777
}
},
query: {
api_key: api_key
}
)
end
end
用法示例:
MyApiClient.new(api_key: 'xxxxxxxx').cc_query
当你使用format :json
时,HTTParty会自动设置内容类型并处理JSON编码和解码。我猜这就是你失败的地方。