1

Below is code which sends a post request to a rest API also hosted locally.

const URL ='http://localhost:5000/api/v1.0/tasks/'

function setHIT(HIT_state){
console.log("Sending HIT")
fetch(URL, {
  method: "post",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },

  //make sure to serialize your JSON body
  body: JSON.stringify({
    name: 'Example',
    lastname: 'Example'
  })
})
.then( (response) => { 
   console.log(response)
});

}

Previously I was getting CORS request errors, but solved it by disabling Chromium's web security option. GET requests work, however when the code above is called the website receives the following response:

Response {type: "cors", url: "http://localhost:5000/api/v1.0/tasks/", redirected: false, status: 404, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "NOT FOUND"
type: "cors"
url: "http://localhost:5000/api/v1.0/tasks/"
__proto__: Response

The odd things is that there are some requests logged in a database, which means it worked at some point. However I'm not sure what has changed since. What am I missing?

2 Answers 2

1

your code structure using a flask cors you should know that.

from flask import Flask
from flask_cors import CORS

cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app = Flask(__name__)
CORS(app)

@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello, cross-origin-world! this is Handel cors with all route URL  you"

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

3 Comments

There's a missing an include for cross_origin in the code. Regardless the issue is still there. I do however see the request getting to the API, so I'll check if it's not an issue with the URL being passed
So there were some minor errors and now I get a code 201 (That's what is being set in the API). However I don't see the information the API is supposed to be sending back.
HTTP Status 201 (Created) HTTP Status 201 indicates that as a result of HTTP POST request, one or more new resources have been successfully created on server. now you need to return a data with response.
0

what is your backed API?

header('Access-Control-Allow-Origin: http://localhost:5000');

if there is you managing post header from server-side ACL then you need to define a header like Access-Control-Allow-Origin: *
* means all domain

1 Comment

The backend is running on Python (Flask). However the same backend works for GET requests. Is it possible it could work for GET requests and POST requests?

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.