【问题标题】:How can I past a variable in request with HTTParty?如何通过 HTTParty 传递请求中的变量?
【发布时间】:2019-12-06 20:51:39
【问题描述】:

我需要使用 Post 生成一个值并在查询中传递这个值并删除。这该怎么做?

是否可以在请求get或delete的def retrieve方法中直接传递变量的值? I want to use the same value generated in the var that stores the faker gem and pass both get and delete.

require 'HTTParty'
require 'httparty/request'
require 'httparty/response/headers'

class Crud    
  include HTTParty

  def create 
    @@codigo = Faker::Number.number(digits: 5)
    @nome    = Faker::Name.first_name
    @salario = Faker::Number.decimal(l_digits: 4, r_digits: 2)
    @idade   = Faker::Number.number(digits: 2)

    @base_url  = 'http://dummy.restapiexample.com/api/v1/create'

    @body = {
      "id":@@codigo,  
      "name":@nome,
      "salary":@salario,
      "age":@idade        
    }.to_json

    @headers = {
        "Accept": 'application/vnd.tasksmanager.v2',
        'Content-Type': 'application/json'
    }

    @@request = Crud.post(@base_url, body: @body, headers: @headers) 
 end

  def retrieve
    self.class.get('http://dummy.restapiexample.com/api/v1/employee/1') 
  end 
end

【问题讨论】:

    标签: ruby httparty


    【解决方案1】:

    只需解析来自 API 的响应并使用获取的 id。创建员工时不需要传递id,它是自动生成的

    class Crud
      include HTTParty
      base_uri 'http://dummy.restapiexample.com/api/v1'
    
      def create 
        nome    = Faker::Name.first_name
        salario = Faker::Number.decimal(l_digits: 4, r_digits: 2)
        idade   = Faker::Number.number(digits: 2)
        #note, you should pass body as JSON string
        body = { name: nome, salary: salario, age: idade }.to_json
    
        headers = {
          'Accept' => 'application/vnd.tasksmanager.v2',
          'Content-Type' => 'application/json'
        }
    
        self.class.post('/create', body: body, headers: headers) 
      end
    
      def retrieve(id)
        self.class.get("/employee/#{ id }")
      end 
    end
    
    > client = Crud.new 
    > response = client.create
    > id = JSON.parse(response)['id']
    > client.retrieve(id)
    

    请阅读 ruby​​ 中的变量 - 局部变量、实例变量和全局变量之间的区别。全局变量应在极少数情况下使用,更多情况下您需要实例/本地变量。

    【讨论】:

    • 首先你是个天使!我会测试这个解决方案并向你保证我会努力学习!我相信这个解决方案也可以删除对吗?。
    • @saulik 谢谢 :) 现在我明白了,你需要什么,我已经更新了答案。 delete 的效果相同
    • Vasi,和你一样改了,请求 Create 工作正常,但是查询失败。创建:{"name":"Donovan","salary":"3204.98","age":"34","id":"126446"} -- OK 检索:self.class.get("/employee/ #{id}") 显示如下错误:"wrong number of arguments (given 0, expected 1) (ArgumentError)" ..thank you,Thank you....
    • @saulik 看起来您在调用retrieve 时忘记传递id。请再次检查我的答案 - client.retrieve(id)。关于 TW - 这不是 StackOverflow 的工作方式,主要目标是分享知识
    • 我不知道为什么但是返回的id总是在当前ID之后的一个数字。我又跑了: _manter_user.create expect(_manter_user.create.code).to eq (200) puts _manter_user.create.body response = _manter_user.create puts id = JSON.parse(response)['id'] {"name" :"Elsie","salary":"4159.51","age":"45","id":"136118"} - ID 请求 136119 - 恢复 ID
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2017-12-27
    • 2018-09-20
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    相关资源
    最近更新 更多