Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Your API Works in cURL. Why Does the Browser Reject It?

The API returns JSON in your terminal. Your frontend gets a network error. Before changing the payload again, compare the two requests: the browser may be asking permission to send a request that cURL sends directly. That difference gives you a useful starting point. Keep the successful request as a baseline, then investigate the browser exchange separately. Find the request that actually failed…

The API functions correctly when accessed via cURL in the terminal, but encounters a network error in the browser. Before altering the payload, compare the two requests closely. The browser may be requesting permission for a request that cURL handles directly. Document the successful request as a baseline before examining the browser's exchange individually. Locate the failed request by opening the browser's Network panel, reproducing the failure, and searching for OPTIONS immediately before the API call.

Ensure the preflight request, which includes the intended method and requested headers, is verified. Document the page origin, destination URL, intended method, requested headers, and the first failing status, including where the failure originated. To effectively diagnose the issue, reproduce the preflight request alongside the POST request.

For instance, replace example URLs with your own development environment using curl -i -X OPTIONS <your API URL> -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: authorization,content-type".

A successful preflight response should include HTTP/1.1 204 No Content, Access-Control-Allow-Origin: https://app.example.com, Access-Control-Allow-Methods: POST, Access-Control-Allow-Headers: authorization, content-type, and Vary: Origin.

After confirming the preflight, examine the actual response. A 401 status code without the required CORS headers may obscure the error the frontend must handle. For cookie-based cross-origin requests, ensure the server allows credentials and returns the specific allowed origin rather than *. Remember that cookie policies still apply.

Avoid using no-cors mode, as it generates an opaque response that cannot be read like an ordinary JSON response. Stick to the Fetch standard's behaviors. Maintain a tiny reproduction package containing the working request, preflight probe, and browser failure details, redacting tokens and cookies. Record the page origin, including its port, to allow others to reproduce the same conditions.

Powerduck, an OpenAPI workspace, can help keep the baseline request and contract together. Finally, utilize browser Developer Tools to confirm that the frontend's CORS configuration is correct, as a successful desktop request does not guarantee proper CORS setup on the website. Only after the real browser can read both a successful response and an expected error should the issue be considered resolved.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Your Custom ESP32 Board Probably Fails on the PCB, Not in the Firmware

The first custom ESP32 board most people make works on the bench and misbehaves everywhere else. The Wi-Fi range is half what the dev board had. The chip resets the moment it tries to connect.

  • 80% of ESP32 board failures result from PCB layout issues, not firmware problems
  • Incorrect strapping pin configuration and USB-C power issues are additional frequent failure causes

"iPhone storage full" but your photos are in iCloud? What's actually going on

It's one of the most confusing iPhone messages: you turned on iCloud Photos, you pay for 200 GB or 2 TB, and your phone still says storage almost full .

  • iPhone storage appears full despite iCloud photos enabled
  • Optimize iPhone Storage keeps full-resolution photos in iCloud
  • SnapClean identifies local storage issues and compresses files

I analyzed [12,347] love letters without opening a single one

I run two letter sites. Digital Letter , where you pick a design, write a note, seal it in a digital envelope, and send it with a link.

  • Author analyzed 12,347 sealed letters and 3,812 open-when envelopes
  • Median letter contains 89 words, with emojis averaging 5.1 per letter
  • Open-when letters often express difficult emotions like apologies

More from Friday 9 October →