johnmelodyme

johnmelodyme

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

Showing Posts 1 to 4

NobbZ

NobbZ

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

johnmelodyme

johnmelodyme OP

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:

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.

johnmelodyme

johnmelodyme OP

@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
 }}
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews