typicode / jsonplaceholder

A simple online fake REST API server
https://jsonplaceholder.typicode.com
MIT License
5.05k stars 582 forks source link

Can you simulate a delay? #198

Closed nonopolarity closed 7 months ago

nonopolarity commented 1 year ago

Can you possibly add a delay so we can do some debugging, such as

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?delay=5`)

so the above can do a 5 seconds delay? Maybe the range can be 0 to 5 so that it doesn't stress your server out. (such as 30 seconds delay which can be a lot of requests bottled up). Alternatively if you want to support millisecond, then maybe

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?delay=3000`)

This is for example, if we want to debug / test a slow response, and the user already click on a new link or image that fetches that data faster than the earlier response, so that the earlier response should really be ignored or canceled by our app.

cmsong-shina commented 8 months ago

use _dealy

100

frankfuu commented 7 months ago

Is this documented anywhere? I can't find any documentation around this even though it works

nonopolarity commented 7 months ago

Is this documented anywhere? I can't find any documentation around this even though it works

use _dealy

100

so I think you mean use _delay

so it will be

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?_delay=5`)
nonopolarity commented 7 months ago

P.S. The line

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?_delay=5000`)

sometimes has the delay, but sometimes not. (so I am not even sure if it was just a random network delay)

It needs to be 5000 to mean 5 seconds

cmsong-shina commented 7 months ago

P.S. The line

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?_delay=5000`)

sometimes has the delay, but sometimes not. (so I am not even sure if it was just a random network delay)

It needs to be 5000 to mean 5 seconds

Might be they optimize ip-base cahce to prevent some DDoS attack.

You can add randome parameter and it should works. (I've just tested with curl)

fetch(`https://jsonplaceholder.typicode.com/users/${userID}?_delay=5000&dummy=${randomNumber()}`)

And yes, it is very strange nothing documented about this.

typicode commented 7 months ago

Hi,

JSONPlaceholder doesn't support _delay. So addind it does nothing actually.

You can however use the CLI locally with v0.17 json-server --delay 5000 db.json https://github.com/typicode/json-server/tree/v0.17.4

Or use Chrome's Network tab > throtling to delay requests.

UmarBaraq commented 3 weeks ago

You can use DummyJSON for this: https://dummyjson.com/docs#intro-delay

fetch('https://dummyjson.com/users/1/?delay=1000');