小编Nag*_*aga的帖子

Ruby Rest 客户端 POST,如何发送标头

Ruby rest 客户端无法发送标头,当我触发来自 ruby​​ 的 post 请求时,我的 java 服务无法读取标头,如下所示。我的服务层抛出错误 Header companyID is missing。在 Postman 中运行相同的 http 请求时,它可以工作。

response = RestClient::Request.new({
      method: :post,
      url: 'https://example.com/submitForm',
      headers:{content_type: 'application/x-www-form-urlencoded',
              companyID:'Company1',
              Authorization:'Basic HELLO1234'},
      payload: { data: str_res}
    }).execute do |response, request, result|
      case response.code
      when 400
        [ :error, JSON.parse(response.to_str) ]
      when 200
        [ :success, JSON.parse(response.to_str) ]
        puts request.headers 
      else
        fail "Invalid response #{response.to_str} received."
      end
    end
Run Code Online (Sandbox Code Playgroud)

这是有效的邮递员代码。在 Ruby Rest 中需要类似的,请提供建议。

POST /submitForm HTTP/1.1
Host: example.com
companyID: Company1
Authorization: Basic HELLO1234
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails ruby-2.0 ruby-on-rails-5

3
推荐指数
1
解决办法
9677
查看次数

NodeJS将请求主体转换为大写并返回响应

使用下面的代码我计划将req主体转换为大写并返回res并且还打印它stdout,当我测试res从未被提取,并且http连接永远存在.请有人在这里指出错误.

var http =require('http'); 
var fs = require('fs');
var through = require('through2');

var server = http.createServer(function(req,res){
             if(req.method==='POST'){
                req.pipe(through(function(buffer,encoding,next){
                        //console.log()
                        this.push(buffer.toString().toUpperCase());
                        next();
                    })).pipe(process.stdout).pipe(res) // If remove .pipe(process.stdout) then I get response.

             }
             //res.end(); -- If I enable this I do get a blank response with 200 OK
            })

server.listen(process.argv[2]);
Run Code Online (Sandbox Code Playgroud)

node.js sails.js

2
推荐指数
1
解决办法
9339
查看次数