I'm trying to make a request to my local API from the Swagger documentation, but it always gives this error when I make the request
These are two different types that are being used for your key.
%{"key" => 1}
is using a string for a key.%{key: 1}
is short hand for %{:key => 1}
which is using an atom for a key.You can only use map.key
syntax if you have atoms for keys.
Both ways of accessing the values are syntatic sugar for Map.get/2 function.
So the difference between the two is that atoms are not garbage collected, and they are optimized by the erlang VM for things like pattern matching.
Strings are more general purpose, so because optimizations you should always try to use atoms as keys, if you can.
Now I don't know if I understood your last question, but a good "erlang" way of getting the key you want in that function would be:
as atom:
def fun1(%{key: value}) do
value
end
as string:
def fun1(%{"key" => value}) do
value
end
In both cases it will only enter the function if it matches the pattern, and your value will be already available inside the function.
First check that your address is not blocked by cors, for dev tests you can use Access-Control-Allow-Origin:*
.
If it doesn't work for you, check that you are not using an extension on your browser, such as those that block ads. You delete it from your browser, restart it and test again, it will work.
In my case, I was accessing the web application via 127.0.0.1:3000, once I changed it to localhost:3000, it worked, hope that helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With