简体   繁体   English

如何格式化 HTTParty POST 请求?

[英]How to format HTTParty POST request?

I've been playing around with API calls for a project I'm working on and I'm getting a problem when I try to pass some JSON to a POST request.我一直在为我正在处理的项目处理 API 调用,当我尝试将一些 JSON 传递给 POST 请求时遇到了问题。 The call works in Postman, but I just can't figure out how to format it in Ruby.该调用在 Postman 中有效,但我不知道如何在 Ruby 中对其进行格式化。 Here's my code:这是我的代码:

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()

This code gives me this error:这段代码给了我这个错误:

{"fault"=>
  {"faultstring"=>"Failed to execute the ExtractVariables: Extract-Variables",
   "detail"=>{"errorcode"=>"steps.extractvariables.ExecutionFailed"}}}

I know that error because I would get it if I tried to make a call without any JSON in the body of the call (through Postman).我知道这个错误,因为如果我尝试在调用主体中没有任何 JSON 的情况下进行调用(通过 Postman),我会得到它。 So from that I assume that my code above is not passing any JSON when making an API call.因此,我假设我上面的代码在进行 API 调用时没有传递任何 JSON。 Am I formatting my JSON incorrectly?我是否错误地格式化了我的 JSON? Am I even formatting the .post call correctly?我什至正确格式化 .post 调用吗? Any help is appreciated!任何帮助表示赞赏! :) :)

the api_test() method just makes a POSt call to example.com and it works (saving my sanity). api_test() 方法只是对 example.com 进行 POST 调用并且它可以工作(保存我的理智)。

Just use HTTParty as a mixin instead in a class instead:只需使用 HTTParty 作为 mixin 而不是在类中:

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

Example usage:用法示例:

MyApiClient.new(api_key: 'xxxxxxxx').cc_query

When you use format :json HTTParty will automatically set the content type and handle JSON encoding and decoding.当您使用format :json HTTParty 时,会自动设置内容类型并处理 JSON 编码和解码。 I'm guessing thats where you failed.我猜这就是你失败的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM