0

Have the next problem:

I get some post queries with the body (on http://api1.com/products) and validate it:

products = JSON.parse(request.body.read)
JSON::Validator.validate!(products_schema, products)

If body data is validated, I try to resend it to another API (http://api2.com/products) with Faraday:

f_headers = { ... }
conn = Faraday.new(headers: f_headers) do |f|
      f.request  :url_encoded
      f.response :logger
      f.adapter  Faraday.default_adapter
    end

resp = conn.post('http://api2.com/products') do |req|
  req.body = request.body.read
end
resp.body

But if I try get body on POST http://api2.com/products, I get body data in params:

[127.0.0.1] [2021-08-20 09:37:08 +0300]
Parameters: {"\n    \"746227ea-5232-11e1-8704-00155d01cd01\",\n    \"d72144ec-8b03-11e1-8b38-00155d01cd01\"\n"=>nil, "url_param"=>"9948f4a4-47c2-11e1-b0a6-00155d01cd01"}

and body is empty if I try: JSON.parse(request.body.read) I get:

809: unexpected token at ''

How properly resent body?

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62

1 Answers1

0

Closed!

Faraday by default sends headers:

Content-Type: "application/x-www-form-urlencoded"

I changed to:

Content-Type: "application/json"

And it works.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62