How to construct HTTPoison requests with dynamic values?

defmodule Prototyping do

  def main() do
    order_type = "sell"
    amount = 1567516
    price = 1351965

    request = %HTTPoison.Request{
      method: :post,
      url:         "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams",
      options: [],
      headers: [
        {~s|accept|, ~s|application/json|},
        {~s|Content-Type|, ~s|application/json|}
      ],
      params: [],
      body: ~s|{"orderType": #{order_type}, "amount": #{amount}, "price": #{price}}|
    }


    test_request2 = %HTTPoison.Request{
      method: :post,
      url:
        "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams",
      options: [],
      headers: [
        {~s|accept|, ~s|application/json|},
        {~s|Content-Type|, ~s|application/json|}
      ],
      params: [],
      body: ~s|{"orderType": "sell", "amount": 1567516, "price": 1351965}|
    }

    HTTPoison.request(test_request2) |> IO.inspect()
    HTTPoison.request(request) |> IO.inspect()
  end
end

HTTPoison.request(test_request2) returns:

{:ok,
 %HTTPoison.Response{
   body: "{\"base\":{\"feeAssetId\":\"WAVES\",\"matcherFee\":1000000},\"discount\":{\"feeAssetId\":\"Atqv59EYzjFGuitKVnMRk6H8FukjoV3ktPorbEys25on\",\"matcherFee\":2661033}}",
   headers: [
     {"matcher-http-server", "matcher-1"},
     {"Access-Control-Allow-Origin", "*"},
     {"Access-Control-Allow-Credentials", "true"},
     {"Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"},
     {"Access-Control-Allow-Headers",
      "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,signature,timestamp"},
     {"Access-Control-Expose-Headers", "Content-Length,Content-Range"},
     {"Backend", "unknown"}
   ],
   request: %HTTPoison.Request{
     body: "{\"orderType\": \"sell\", \"amount\": 1567516, \"price\": 1351965}",
     headers: [
       {"accept", "application/json"},
       {"Content-Type", "application/json"}
     ],
     method: :post,
     options: [],
     params: [],
     url: "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams"
   },
   request_url: "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams",
   status_code: 200
 }}

but HTTPoison.request(request) returns a 404:

{:ok,
 %HTTPoison.Response{
   body: "The requested resource could not be found but may be available again in the future.",
   headers: [
     {"matcher-http-server", "matcher-1"},
     {"Access-Control-Allow-Origin", "*"},
     {"Access-Control-Allow-Credentials", "true"},
     {"Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"},
     {"Access-Control-Allow-Headers",
      "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,signature,timestamp"},
     {"Access-Control-Expose-Headers", "Content-Length,Content-Range"},
     {"Backend", "unknown"}
   ],
   request: %HTTPoison.Request{
     body: "{\"orderType\": sell, \"amount\": 1567516, \"price\": 1351965}",
     headers: [
       {"accept", "application/json"},
       {"Content-Type", "application/json"}
     ],
     method: :post,
     options: [],
     params: [],
     url: "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams"
   },
   request_url: "https://matcher.waves.exchange/matcher/orderbook/WAVES/DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p/calculateFee#calculateFeeByAssetPairAndOrderParams",
   status_code: 404
 }}

How should the request be written to accommodate dynamic values?

  1. You’ve forgotten quotes where you do interpolation
  2. I think you’ve shown some private information in this post
  3. Use Poison, Jason or any other JSON encoder
1 Like