Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST methods broken #165

Open
ink-pot-monkey opened this issue Mar 18, 2024 · 4 comments
Open

POST methods broken #165

ink-pot-monkey opened this issue Mar 18, 2024 · 4 comments

Comments

@ink-pot-monkey
Copy link

First let me say thanks for this great API.

I've noticed while playing around with it that all the POST methods seem to be broken i.e.:

❯ curl --location 'https://countriesnow.space/api/v0.1/countries/states' \
--data '{
    "country": "Nigeria"
}'
{"error":true,"msg":"missing param (country or iso2)"}

I've got these requests to work with the query string, IE

❯ curl --location 'https://countriesnow.space/api/v0.1/countries/states/?country="Nigeria"'

So I am guessing something is going wrong with the Middleware redirection in

* Redirect all POST requests by appending "/q" to the end of the route

Is there anyway of investigating this further? I've cloned the repo and the tests pass, which contradicts my lived experience.

Alternatively, is it time to remove all the POST requests (at least in the documentation) for their proper GET versions? I'm happy to take this on

@MartinsOnuoha
Copy link
Owner

Hi @thomassdk, Thanks for reporting this; I tried replicating this problem on postman but can't. the post requests seem to be working just fine.
CleanShot 2024-03-24 at 4  26 47@2x

@ink-pot-monkey
Copy link
Author

The context I am trying to use the api in is the browser where the fetch request is also broken, here is the minimal example gotten from the docs:

var raw = '{\n    "country": "nigeria"\n}';

var requestOptions = {
  method: "POST",
  body: raw,
  redirect: "follow",
};

fetch("https://countriesnow.space/api/v0.1/countries/states", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error));

Which gives me the following error

❯ bun api.js
{"error":true,"msg":"missing param (country or iso2)"}

Adding the query parameter returns the correct result

var raw = '{\n    "country": "nigeria"\n}';

var requestOptions = {
  method: "GET",
};

fetch(
  'https://countriesnow.space/api/v0.1/countries/states?country="Nigeria"',
  requestOptions,
)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error));

Confused as to why postman is working correctly...

@MartinsOnuoha
Copy link
Owner

Would you like to update the documentation with the GET equivalent in the interim while we figure out what's wrong with the POST requests?

@MartinsOnuoha
Copy link
Owner

@ink-pot-monkey feel free to open an MR for this fix 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants