infinitered / apisauce

Axios + standardized errors + request/response transforms.
MIT License
2.79k stars 184 forks source link

XMLHttpRequest #130

Closed Chatharaju closed 6 years ago

Chatharaju commented 6 years ago

Can anyone please tell how to use this library for XMLHttpRequest for GET

This is the endpoint I need to hit: http://webservices.nextbus.com/service/publicXMLFeed?command=routeList

skellock commented 6 years ago

What have you tried so far?

Chatharaju commented 6 years ago

Am all new to this scene. This is what I have so far. Am getting status code as 0 with empty response.

import apisauce from 'apisauce'

const create = (baseURL = 'http://webservices.nextbus.com/service/publicXMLFeed?') => { const api = apisauce.create({ baseURL,

headers: {
  'Accept': 'text/xml',
  'Cache-Control': 'no-cache'
},

timeout: 10000

}) const getRouteList = () => api.get('command=routeList&a=sf-muni') return { getRouteList } } export default { create }

skellock commented 6 years ago

Try a baseURL of http://webservices.nextbus.com/service and your get request as:

api.get('publicXMLFeed', { command: 'routeList', a: 'sf-muni' })

Chatharaju commented 6 years ago

Yes, I have modified my get request exactly what you have ending up getting Status code as 0.

skellock commented 6 years ago

Does that work if you use curl or postman?

Chatharaju commented 6 years ago

Yes, url gives data back on Postman. I have tested that.

skellock commented 6 years ago

What environment are you using?

Chatharaju commented 6 years ago

Environment: OS: macOS Sierra 10.12.6 Node: 8.4.0 npm: 5.3.0

Packages: "react": "16.0.0-beta.5", "react-native": "0.49.3",

skellock commented 6 years ago

On iOS you can't use http without adding an exception. Did you do this in your info.plist?

https://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

Chatharaju commented 6 years ago

Yes, I have it already on my info.plist, still didnt work.

I have also updated with "NSAllowsArbitraryLoads", which allows all connections but no luck.

skellock commented 6 years ago

Are you able to paste a minimal reproducible version current source code somewhere so I can try this out?

maoueh commented 6 years ago

@Chatharaju A working example

In your project, add a file named test_api.js and put the following content:

const apisauce = require("apisauce");

const baseURL = "http://webservices.nextbus.com/service";
const api = apisauce.create({
  baseURL,
  headers: {
    Accept: "text/xml",
    "Cache-Control": "no-cache"
  }
});

api.get("/publicXMLFeed", { command: "routeList", a: "sf-muni" }).then(response => {
  console.log(`Response code: ${response.status}`);
  console.log("Response body");
  console.log(response.data);
});

Then to run it, do:

node test_api.js

It printed this for me:

Response code: 200
Response body
<?xml version="1.0" encoding="utf-8" ?>
<body copyright="All data copyright San Francisco Muni 2017.">
<route tag="E" title="E-Embarcadero"/>
<route tag="F" title="F-Market &amp; Wharves"/>
<route tag="J" title="J-Church"/>
<route tag="KT" title="KT-Ingleside/Third Street"/>
<route tag="L" title="L-Taraval"/>
<route tag="M" title="M-Ocean View"/>
<route tag="N" title="N-Judah"/>
<route tag="NX" title="NX-Express"/>
<route tag="1" title="1-California"/>
<route tag="1AX" title="1AX-California A Express"/>
<route tag="1BX" title="1BX-California B Express"/>
<route tag="2" title="2-Clement"/>
<route tag="3" title="3-Jackson"/>
<route tag="5" title="5-Fulton"/>
<route tag="5R" title="5R-Fulton Rapid"/>
<route tag="6" title="6-Haight-Parnassus"/>
<route tag="7" title="7-Haight-Noriega"/>
<route tag="7X" title="7X-Noriega Express"/>
<route tag="8" title="8-Bayshore"/>
<route tag="8AX" title="8AX-Bayshore A Express"/>
<route tag="8BX" title="8BX-Bayshore B Express"/>
<route tag="9" title="9-San Bruno"/>
<route tag="9R" title="9R-San Bruno Rapid"/>
<route tag="10" title="10-Townsend"/>
<route tag="12" title="12-Folsom-Pacific"/>
<route tag="14" title="14-Mission"/>
<route tag="14R" title="14R-Mission Rapid"/>
<route tag="14X" title="14X-Mission Express"/>
<route tag="18" title="18-46th Avenue"/>
<route tag="19" title="19-Polk"/>
<route tag="21" title="21-Hayes"/>
<route tag="22" title="22-Fillmore"/>
<route tag="23" title="23-Monterey"/>
<route tag="24" title="24-Divisadero"/>
<route tag="24MC" title="24MC-24 Divisadero Substitution"/>
<route tag="25" title="25-Treasure Island"/>
<route tag="27" title="27-Bryant"/>
<route tag="28" title="28-19th Avenue"/>
<route tag="28R" title="28R-19th Avenue Rapid"/>
<route tag="29" title="29-Sunset"/>
<route tag="30" title="30-Stockton"/>
<route tag="30X" title="30X-Marina Express"/>
<route tag="31" title="31-Balboa"/>
<route tag="31AX" title="31AX-Balboa A Express"/>
<route tag="31BX" title="31BX-Balboa B Express"/>
<route tag="33" title="33-Ashbury-18th St"/>
<route tag="35" title="35-Eureka"/>
<route tag="36" title="36-Teresita"/>
<route tag="37" title="37-Corbett"/>
<route tag="38" title="38-Geary"/>
<route tag="38R" title="38R-Geary Rapid"/>
<route tag="38AX" title="38AX-Geary A Express"/>
<route tag="38BX" title="38BX-Geary B Express"/>
<route tag="39" title="39-Coit"/>
<route tag="41" title="41-Union"/>
<route tag="43" title="43-Masonic"/>
<route tag="44" title="44-O&apos;Shaughnessy"/>
<route tag="45" title="45-Union-Stockton"/>
<route tag="47" title="47-Van Ness"/>
<route tag="48" title="48-Quintara-24th Street"/>
<route tag="49" title="49-Van Ness-Mission"/>
<route tag="52" title="52-Excelsior"/>
<route tag="54" title="54-Felton"/>
<route tag="55" title="55-16th Street"/>
<route tag="56" title="56-Rutland"/>
<route tag="57" title="57-Parkmerced"/>
<route tag="66" title="66-Quintara"/>
<route tag="67" title="67-Bernal Heights"/>
<route tag="76X" title="76X-Marin Headlands Express"/>
<route tag="81X" title="81X-Caltrain Express"/>
<route tag="82X" title="82X-Levi Plaza Express"/>
<route tag="83X" title="83X-Mid-Market Express"/>
<route tag="88" title="88-Bart Shuttle"/>
<route tag="90" title="90-San Bruno Owl"/>
<route tag="91" title="91-Owl"/>
<route tag="K_OWL" title="K-Owl"/>
<route tag="L_OWL" title="L-Owl"/>
<route tag="M_OWL" title="M-Owl"/>
<route tag="N_OWL" title="N-Owl"/>
<route tag="T_OWL" title="T-Owl"/>
<route tag="59" title="Powell/Mason Cable Car"/>
<route tag="60" title="Powell/Hyde Cable Car"/>
<route tag="61" title="California Cable Car"/>
</body>

The code is working as expected, it you are not getting the same result, you have probably something wrong in your own code.

Hope it helps.

Chatharaju commented 6 years ago

Actually, problem is with iOS, Android works totally fine.

skellock commented 6 years ago

I recommend double checking your arbitrary load settings.