Comments (3)
Hi @ianwalter, the :ok
tuple means: this request didn't fail (connection was not closed, it reached the server, the whole request was transmitted, etc).
The status code is dependent on the application. It may seem clear that 404 is an error, but what about a 303 or a 301? Should they be error
or ok
?
You may even have APIs that always return 200 and putting some error status inside the JSON payload. For example:
$ curl -i "http://api.openweathermap.org/data/2.5/weather?id=21372797&appid=2de143494c0b295cca9337e1e96b00e0"
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 08 Nov 2015 05:32:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Source: redis
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
{"cod":"404","message":"Error: Not found city"}
from httpoison.
HTTP/1.1 200 OK
That 200 is extrange. Why don't send a 404 error? With that 200 OK
response, you have to handle errors in the success function.
What actually I have to do:
import axios from 'axios';
const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=XXXXXXXXXXXXX&units=metric';
export default {
getTemp : function getTemp(location) {
var encodedLocation = encodeURIComponent(location),
requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;
return axios.get(requestUrl).then(function success(res) {
// I have to check inside the success function
if (res.data.cod && res.data.message) {
throw new Error(res.data.message);
}
return res.data.main.temp;
}, function error(res) {
throw new Error(res.data.message);
});
},
};
What could be better:
import axios from 'axios';
const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=XXXXXXXXXXXXX&units=metric';
export default {
getTemp : function getTemp(location) {
var encodedLocation = encodeURIComponent(location),
requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;
return axios.get(requestUrl).then(function success(res) {
return res.data.main.temp;
}, function error(res) {
// Since the header HTTP response is 404, it fails
throw new Error(res.data.message);
});
},
};
It can be possible to you to have a "standard" http header for errors like that? Errors who are not related a malfunction.
Thanks!
from httpoison.
The open weather API is simply wrong. You should not reply 200 when the requested resource doesn't exist. I can't see how the client (HTTPoison) can protect anyone from such broken API.
from httpoison.
Related Issues (20)
- 1.8 not installing HOT 1
- Response HOT 2
- Request is getting closed. HOT 2
- HTTPoison.MaybeRedirect for 308 status_code ? HOT 11
- why no_proxy doesn't support CIDR range format?
- checkout_failure when using httpoison with proxy HOT 3
- Lowercase location header not recognized when constructing %HTTPoison.MaybeRedirect{}
- TLS client: In state wait_cert_cr at ssl_handshake.erl:1984 generated CLIENT ALERT: Fatal - Unknown CA HOT 3
- SSL options replace default options (instead of just being added to them) HOT 2
- Default TLS version? HOT 2
- Proxy option not working HOT 1
- Hackney 1.18? HOT 1
- Overly loose typespecs
- HTTPoison.get! follow_redirect does not work with an async request HOT 1
- Nested form-encoded params don't work HOT 2
- "Path not supported by Algolia REST API" HOT 2
- How to contribute HOT 1
- ipv6 localhost address is passed to hackney without square brackets HOT 1
- Get HTTPoison.Error{reason: {:options, :dependency}} when send request with settings for tlsv1.3
- Issue Case clause error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpoison.