最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Mockoon Instead of MockAPI - POST Request Not Working - Stack Overflow

programmeradmin3浏览0评论

I’m following a tutorial that uses MockAPI to create a mock backend. However, since MockAPI now limits the number of resources in free projects (the tutorial was recorded before this limitation), I switched to Mockoon instead. Unfortunately, my POST requests to the Mockoon server are failing, and I’m not sure why.

When using MockAPI, my POST request works fine:

class User {
    user_id = '';
    username = '';
    email = '';
    api_url = '';

    create() {
        let data = {
            username: this.username,
            email: this.email,
            password: this.password
        };        

        data = JSON.stringify(data);

        fetch(this.api_url + '/user', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: data
        })
        .then(response => response.json())
        .then(data => {
            console.log('User created');
        })
        .catch(error => console.error(error));
    } 
}

However, when I switch to Mockoon, it does not work:

class User {
    user_id = '';
    username = '';
    email = '';
    api_url = 'http://localhost:1269';

    create() {
        let data = {
            username: this.username,
            email: this.email,
            password: this.password
        };        

        data = JSON.stringify(data);

        fetch(this.api_url + '/users', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: data
        })
        .then(response => response.json())
        .then(data => {
            console.log('User created');
        })
        .catch(error => console.error(error));
    } 
}

With MockAPI, my POST request works correctly, but when I switch to Mockoon, I get the following errors in the browser console:

  • POST http://localhost:1269//users 404 (Not Found)
  • Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

What I’ve Tried:

  • Double-checked that Mockoon is running on port 1269.
  • Verified that the endpoint /users exists in my Mockoon API configuration.
  • Tried changing /users to /user, /api/users, and other variations.
  • Checked the Mockoon logs to see if the request is reaching the mock server.

Possible Causes (Need Help!):

  • Is there a specific way Mockoon expects API endpoints to be set up?
  • Does Mockoon handle POST requests differently than MockAPI?
  • How do I define response structures in Mockoon similar to MockAPI’s automatic mock data?

Any help would be greatly appreciated!

发布评论

评论列表(0)

  1. 暂无评论