Home > Blockchain >  What's the difference between these two requests?
What's the difference between these two requests?

Time:02-09

Why does the first request return 200,

fetch("https://api.peopledatalabs.com/v5/autocomplete?api_key=someAPIKey&field=location")

but the second returns 400/401?

fetch(`https://api.peopledatalabs.com/v5/autocomplete`, {
    "api_key": "someAPIKey",
    "field": "location"
    });

CodePudding user response:

In the second way, you passed the option parameter (with "api_key" and "field"), not queryParams as you meant.

You can change it to work using URLSearchParams

fetch('https://api.peopledatalabs.com/v5/autocomplete?'   new URLSearchParams({
    api_key: "someAPIKey",
    field: "location",
}))
  •  Tags:  
  • Related