0

There is a nice post how to create a CORS Fairing. The given solutions worked fine as long as there was no the preflight request (PUT, DELETE). The partial solution I've found is given by Mike Mannakee

It's slightly modify:

async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
        response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
        response.set_header(Header::new("Access-Control-Allow-Methods", "*"));
        response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
        response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
        if request.method() == Method::Options {
            println!("Option => {}", request.uri());
            response.set_status(Status::NoContent);
        }
    }

Unfortunately, DELETE method still doesn't work

    --- put ---
    OPTIONS /api/code_data/m4test/breakpoint:
       >> No matching routes for OPTIONS /api/code_data/m4test/breakpoint.
       >> No 404 catcher registered. Using Rocket default.
    Option => /api/code_data/m4test/breakpoint
       >> Response succeeded.
    PUT /api/code_data/m4test/breakpoint application/json:
       >> Matched: (add_breakpoint_handler) PUT /api/code_data/<game_name>/breakpoint 
    application/json
    Add breakpoint 2
       >> Outcome: Success
       >> Response succeeded.
    
    --- delete ----
    OPTIONS /api/code_data/m4test/breakpoint:
       >> No matching routes for OPTIONS /api/code_data/m4test/breakpoint.
       >> No 404 catcher registered. Using Rocket default.
    Option => /api/code_data/m4test/breakpoint
       >> Response succeeded.
    DELETE /api/code_data/m4test/breakpoint text/plain:
       >> No matching routes for DELETE /api/code_data/m4test/breakpoint text/plain.
       >> No 404 catcher registered. Using Rocket default.
       >> Response succeeded.

rocekt_cors didn't work for me, too.

The preflight req is visible in FF. Not sure how can it be shown in Chrome.

Please could you share you solution if you had a similar issue?

Thunder client, Postman, curl worked wo any issue.

Update:

The issue was "the content type negotiation" - "content-type" was commented out in the first fetch. Everything works as expected:

if (!breakpoint) {
    // delete
    wasm?.update_breakpoints({}, [address]);
    // TODO: handle errors
    await fetch(`http://localhost:8000/api/code_data/${currentGame}/breakpoint`, {
      method: 'delete',
      headers: {
        'content-type': 'application/json',
      },
      body: JSON.stringify({ address }),
    });
  } else {
    // add breakpoint
    wasm?.update_breakpoints({ [address]: breakpoint }, []);
    // TODO: handle errors
    await fetch(`http://localhost:8000/api/code_data/${currentGame}/breakpoint`, {
      method: 'put',
      headers: {
        'content-type': 'application/json',
      },
      body: JSON.stringify({ address, ...breakpoint }),
    });
  }
DraganS
  • 2,621
  • 1
  • 27
  • 40

0 Answers0