Had anyone do a http request in Elixir (mint) for GOIP?

I wrote a server-side programme for GOIP sending OTP number to SMS in Ruby I requested it easily, but how do I request it in elixir ?

require "uri"
require "net/http"

otp_number = rand(000000 .. 999999)
puts "Plese Enter A Phone Number: "
user_phone = gets.chomp
url = URI("http://[my-ip-address]/default/en_US/sms_info.html?line=1&action=sms&telnum=#{user_phone}&smscontent=Application your OTP is #{otp_number}&smskey=1162480578")

http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Post.new(url)
request["Authorization"] = "Basic YWRtaW46YWRtaW4="
request["Accept"] = "*/*"
request["Content-Type"] = "application/x-www-form-urlencoded"
request["Content-Length"] = "125"

response = http.request(request)
puts response.read_body

What have you tried so far and what http library are you using?

I tried with (Mint) here’s what I did :

...
{:ok, conn} = Mint.HTTP1.connect(:http, "#{host}", 80)
    #line=#{line}&action=#{action}&telnum=#{telnum}&smscontent=#{smscontent}&smskey=1162480578
    {:ok, conn, request_ref} = Mint.HTTP1.request(conn, "POST", "/default/en_US/sms_info.html&line=1&action=#{action}&telnum=#{telnum}&smscontent=#{smscontent}&smskey=1162480578",
     [
       {"Authorization", "Basic YWRtaW46YWRtaW4="},
       {"Accept", "*/*"},
       {"Content-Type", "application/x-www-form-urlencoded"},
       {"Content-Length", "125"}
     ], nil)
    receive do
      message -> {:ok, conn, responses} = Mint.HTTP1.stream(conn, message)
      # {:data, ^request_ref, data} = Enum.find(responses, &(match?({:data, _, _}, &1)))
      # IO.puts(responses)
      IO.inspect(responses)
    end
    {:ok, conn} = Mint.HTTP.close(conn)
  end
....

When I run I get this : ** (MatchError) no match of right hand side value: {:error, %Mint.HTTP1{buffer: "", host: "10.0.0.59", mode: :active, port: 80, private: %{}, request: nil, requests: {[], []}, scheme_as_string: "http", socket: #Port<0.6>, state: :open, transport: Mint.Core.Transport.TCP}, %Mint.HTTPError{module: Mint.HTTP1, reason: {:invalid_request_target, "/default/en_US/sms_info.html?line=1&action=sms&telnum=01111134386&smscontent=Monjo: Your Account Verification Code is 246584&smskey=1162480578"}}} (sms 0.1.0) lib/sms.ex:19: SMS.send/0

:blush:

This is just a guess, but I think you need to escape the spaces in the content. Please check how your ruby code actually encodes it and mimic the same behaviour with your elixir code.

Also mint is rather a low-level library as far as I remember, you might want to prefer a higher level library like httpoison, httpotion or tesla.

1 Like

@NobbZ Many apologies I solved it , at the line

{:ok, conn, request_ref} = Mint.HTTP1.request(conn, "POST", "/default/en_US/sms_info.html&line=1&action=#{action}&telnum=#{telnum}&smscontent=#{smscontent}&smskey=1162480578",
     [
       {"Authorization", "Basic YWRtaW46YWRtaW4="},
       {"Accept", "*/*"},
       {"Content-Type", "application/x-www-form-urlencoded"},
       {"Content-Length", "125"}
     ], nil)

which the parametre I stated “nil” which will not give me result. After adjustment it works !

which is now :blush:

{:ok, conn, request_ref} = Mint.HTTP1.request(conn, "POST", "/default/en_US/sms_info.html?",
     [
       {"Authorization", "Basic YWRtaW46YWRtaW4="},
       {"Accept", "*/*"},
       {"Content-Type", "application/x-www-form-urlencoded"},
       {"Content-Length", "125"}
     ], "line=1&action=#{action}&telnum=#{telnum}&smscontent=#{smscontent}&smskey=1162480578")

the output is something like this now

{:ok,
 %Mint.HTTP1{
   buffer: "",
   host: "[my-ip-i-cant-show]",
   mode: :active,
   port: 80,
   private: %{},
   request: %{
     body: nil,
     connection: [],
     content_length: nil,
     data_buffer: [],
     headers_buffer: [],
     method: "POST",
     ref: #Reference<0.1063083407.3741843458.16189>,
     state: :status,
     status: nil,
     transfer_encoding: [],
     version: nil
   },
   requests: {[], []},
   scheme_as_string: "http",
   socket: #Port<0.6>,
   state: :closed,
   transport: Mint.Core.Transport.TCP
 }}
1 Like