johnmelodyme

johnmelodyme

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

Marked As Solved

johnmelodyme

johnmelodyme

@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
 }}

Also Liked

NobbZ

NobbZ

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.

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement