daveaztig14

daveaztig14

How to use curl as input in elixir script?

Hi,

I am totally new in phoenix, elixir, and ecto. I have been studying for a week about basics and all, so far I’m quite amazed and confused at the same time. Anyway, my problem is this, I have a curl request which is:

curl https://sample.com/environment
-H ‘Content-Type: application/json; charset=utf-8’
-d ‘{“ticket”: 1, “ver”: “0.0.1.0_env”, “os”: “android”}’

I want to use the values of the variables “ticket”, “ver” and “os” as an input in my elixir/ecto script which will query in a remote database. (values is changing each request)

I’m sorry for my question if its a little confusing. Please feel free to ask questions so that I can clarify things. And also I want to know it it truly possible.

Any suggestions, help, links and ideas are welcome. I want to eagerly learn this. Thank you.

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

While it is possible to use curl from Elixir directly, it seems like what you actually want to do, is to perform a remote HTTP request and read the results.
Instead of manually using curl, I suggest using a HTTP client library such as HTTPoison, which does much of the request/response parsing and error handling for you.

NobbZ

NobbZ

And now, here I am, understanding it completely different than @idi527 and @Qqwy did…

For me, it seems as if you want to a webserver that actually receives the curl request.

In this case, take a look at phoenix and/or plug.

daveaztig14

daveaztig14

Hi @Qqwy @NobbZ @idiot,

I am so sorry for my late response. Just an update:

Based on the research I’ve made and the ideas and suggestions you’ve given me, I now have a simple elixir app where in it accepts a request like:

curl “http://localhost:8085/pass” -H Content-Type: application/json; charset=utf-8’ -d '{“ticket”: 1, “ver”: “1”, “os”: “1”}'

and the output is:

ticket: 1, ver: 0.0.1.0_develop, os: ios

SQL Query = select * from database where ticket= 1 AND ver= 1 AND os= 1

Thank you for all your help, ideas and suggestions. together with this link https://www.jungledisk.com/blog/2018/03/19/tutorial-a-simple-http-server-in-elixir/.

It really helped me alot.

My next step now is to connect to a database and query the statement using elixir/ecto. However, it is another topic.

Once again. Thank you.

idi527

idi527

:wave:

How do you plan to use curl from within the elixir app? If it’s

System.cmd("curl", ~w[
  https://sample.com/environment
  -H 'Content-Type: application/json; charset=utf-8'
  -d '{"ticket": #{ticket}, "ver": "#{version}", "os": "#{os}"}'
])

then you can #{interpolate} your variables like this.

However, I’d suggest looking into http clients in elixir/erlang, like :httpoison to make http requests instead of curl.

HTTPoison.get("https://sample.com/environment?ticket=#{ticket}&ver=#{version}&os=#{os}")

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement