0

The Curl of the API says that this is the correct format:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "2018062703,2018062703" "http://XX.XX.XX/api/my_url"

This API need parameters like this:

enter image description here

I'm trying to send them with this code:

var data = "2018062703,2018062703";
$.ajax({
    type: 'POST',
    data: data,
    url: 'my/api/url',
    dataType: 'json',
    success: etc...

But I get a 404 not found. In the console of Chrome I got this:

What I'm doing wrong? In the web of the API when I put the parameters as a string "2018062703,2018062703" it works.

enter image description here

5
  • -d "2018062703,2018062703" is not JSON..... Commented Dec 7, 2018 at 17:05
  • 1
    You get a 404 and it is saying your data is wrong? wouldn't it be a 5XX ? Commented Dec 7, 2018 at 17:07
  • Yes, I know it's weird, but with the same URL of the API by Swagger works ok. Commented Dec 7, 2018 at 17:15
  • Look at the answer below and look at your swagger documentation. The parameter key is "filtro". Commented Dec 7, 2018 at 17:16
  • your data type is json in ajax call and you are passing string. convert it to json and try Commented Dec 8, 2018 at 5:28

1 Answer 1

2

Your data parameter in your AJAX call is invalid. It should look like this:

data: { filtro: data }

As it is now, you're trying to call your API with a parameter named 2018062703,2018062703 which has no value.

Sign up to request clarification or add additional context in comments.

6 Comments

Now I have it in that way, but in Chrome console I got in Headers -> Form Data: filtro: 2018062703,2018062703 but still 404 not found. If I change to GET (only to force to have method error) I got ` GET h ttp : //XX.XX.XX/my_api/?filtro=2018062703%2C2018062703 405 (Method Not Allowed)` which is correct
A 404 most likely means the URL you're calling is wrong. If you're 100% sure the URL is correct, it might be a bit overkill but I would record the call from Swagger and the call you're making in AJAX with a tool like Fiddler and compare them. That way, you'll see exactly what's happening.
I tried with GET method in the ajax, if the URL wasn't correct I would get a 404 too, but instead I get the 405 Method Not Allowed wich is fine.
Finally I had to JSON.stringify() my data, plus, add the contentType: "application/json",``
I will pick you answer as the correct one because it leads me to rewrite my code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.