rayandfz

rayandfz

HTTP Request formurlencoded problem

Hello,

I’m new to Elixir and I come from the world Javascript

To learn the language, I decided to redo a reverse API that I had done a few days ago in JS.

import fetch from 'node-fetch' 

export default class Authentification {
    async login() {
        return (await fetch("https://api.ecoledirecte.com/v3/login.awp", {
            "headers": {
                "accept": "application/json, text/plain, */*",
                "accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
                "content-type": "application/x-www-form-urlencoded",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-site"
            },
            "referrer": "https://www.ecoledirecte.com/login",
            "referrerPolicy": "no-referrer-when-downgrade",
            "body": "data={\n    \"identifiant\": \"" + process.env.USERNAME + "\",\n    \"motdepasse\": \"" + process.env.PASSWORD + "\"\n}",
            "method": "POST",
            "mode": "cors"
        })).json()
    }
}

My problem being that I’m trying to do a code style but I get an error message from the api telling me that the body is incorrect, I don’t really know how to do it, could you direct me?

Would you have a good resource to understand Elixir and the OTP? I come from the imperative world and I have a lot of pain making effective code in Elixir.

  def main() do
       HTTPoison.post "https://api.ecoledirecte.com/v3/login.awp", "{\"body\": data={\"identifiant\": \"secret\"\n    \"motdepasse\": \"secret\"\n}}", [{"Content-Type", "application/x-www-form-urlencoded"}]
  end

Most Liked

al2o3cr

al2o3cr

I don’t think you want the body part here - that was from the JS, not something that should be sent to the API.

The data= bit here is weird, but matches the observed behavior and the behavior described in this post:

The format this is expecting is odd; it appears to be a URL-encoded payload containing exactly one key data containing incompletely-escaped JSON. Prettified from that login page’s source:

        if (config.data && "object" == typeof config.data) {
            config.data.token = $sessionStorage.token;
            var replacer = function(key, value) {
                return "string" == typeof value ? (value = value.replace(/%/g, "%25"),
                value = value.replace(/&/g, "%26"),
                value = value.replace(/\+/g, "%2B")) : value
            }
              , strVal = JSON.stringify(config.data, replacer, 4);
            config.data = "data=" + strVal
        }

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement