You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issue found when installing 'request' package #495
What is your use case for request in a client-side app?
If you just need to make an AJAX request, use window.fetch instead.
We include a polyfill for it.
I want to consume an external server api (not a localhost one, but on another external domain).
I will go the fetch() way then, didn't know about the polyfill feature. Thanks!
fetch() works with any domain as long as CORS is configured on it.
And if it's not, we let you specify "proxy" option in package.json which will automatically proxy all unrecognised requests to the specified server.
You can find more instructions in the generated README of your project, or in template/README.md in this repo.
Hello @gaearon,
I have tried this from a couple of angles, but could not make it to work.
I set up the proxy url, constructed my query call, yet still the fetched url has http://localhost:3000 prepended to the api call. Image below:
In summary: I am setting the proxy url to: "proxy": "https://holidayapi.com/v1", then added the string /api/ in the fetch() call, appended my constructed query, and it did not work ok.
You may see the code in my react-date-range repo, use-fetch branch.
You may check the forms.js file, and the package.json one.
We don’t treat URLs starting with /api in any special way. We just prepend the proxy URL to the URL you pass to fetch().
So it tried to load https://holidayapi.com/v1/api/holidays because that’s what you get by concatenating https://holidayapi.com/v1 and /api/holidays. You probably meant fetch('/holidays') instead.
You are right indeed!
I (mistakenly) was thinking that somehow /api/ was a keyword that would be replaced by the url set in proxy field, in package.json.
Thank you heaps for the given code correction. Now it all works as expected!
Activity
gaearon commentedon Aug 25, 2016
What is your use case for
requestin a client-side app?If you just need to make an AJAX request, use
window.fetchinstead.We include a polyfill for it.
javlc commentedon Aug 25, 2016
I want to consume an external server api (not a localhost one, but on another external domain).
I will go the fetch() way then, didn't know about the polyfill feature. Thanks!
gaearon commentedon Aug 26, 2016
fetch() works with any domain as long as CORS is configured on it.
And if it's not, we let you specify "proxy" option in package.json which will automatically proxy all unrecognised requests to the specified server.
You can find more instructions in the generated README of your project, or in
template/README.mdin this repo.javlc commentedon Aug 28, 2016
Hello @gaearon,
I have tried this from a couple of angles, but could not make it to work.
I set up the proxy url, constructed my query call, yet still the fetched url has http://localhost:3000 prepended to the api call. Image below:
In summary: I am setting the proxy url to:
"proxy": "https://holidayapi.com/v1", then added the string/api/in the fetch() call, appended my constructed query, and it did not work ok.You may see the code in my react-date-range repo, use-fetch branch.
You may check the forms.js file, and the package.json one.
gaearon commentedon Aug 28, 2016
You don’t need to. Why did you do this?
We don’t treat URLs starting with
/apiin any special way. We just prepend the proxy URL to the URL you pass tofetch().So it tried to load
https://holidayapi.com/v1/api/holidaysbecause that’s what you get by concatenatinghttps://holidayapi.com/v1and/api/holidays. You probably meantfetch('/holidays')instead.gaearon commentedon Aug 28, 2016
Verified that this fixes the problem in your repo:
function search(query) { - return fetch(`/api/holidays?${query}`, { + return fetch(`/holidays?${query}`, { accept: 'application/json', }).then(checkStatus) .then(parseJSON); @@ -129,7 +129,7 @@ class Form extends Component { return response.json(); } - search(querystring); + search(querystring).then(result => console.log(result)); }javlc commentedon Aug 29, 2016
You are right indeed!
I (mistakenly) was thinking that somehow /api/ was a keyword that would be replaced by the url set in proxy field, in package.json.
Thank you heaps for the given code correction. Now it all works as expected!