Jsonstub response not showing

2019-09-07 08:36发布

axios.get('http://jsonstub.com', {
  headers: {
    'Content-Type': 'application/json',
    'JsonStub-User-Key': '1699b7fc-cdcb-4205-b30a-b9b3626246c5',
    'JsonStub-Project-Key': 'f39f1bd2-d303-4b7e-9a11-c0f049a46f87'
}
}).then((data) => console.log(data))

I have set up jsonstub, but when I try making a request with axios, I get status 200 OK, with html response. When I open that html it shows me "Page not found". Doing this with Postman gives me back my json. I'm doing this from localhost:8080

This is content of html: "

<!DOCTYPE html>
         <html>
         <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Jsonstub</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <base href="/" />
<meta name="jsonstub-ember/config/environment" content="%7B%22modulePrefix%22%3A%22jsonstub-ember%22%2C%22environment%22%3A%22production%22%2C%22baseURL%22%3A%22/%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%7D%2C%22APP%22%3A%7B%22API%22%3A%7B%22devMode%22%3Afalse%2C%22host%22%3A%22https%3A//jsonstub.com%22%2C%22namespace%22%3A%22api%22%7D%2C%22STRIPE%22%3A%7B%22pubKey%22%3A%22pk_B95JCBxoUTpYXMGMKuPj0TTKDq30k%22%7D%7D%2C%22simple-auth%22%3A%7B%22authorizer%22%3A%22simple-auth-authorizer%3Aoauth2-bearer%22%2C%22crossOriginWhitelist%22%3A%5B%22http%3A//jsonstub.dev%22%2C%22http%3A//jsonstub.com%22%5D%2C%22routeAfterAuthentication%22%3A%22projects%22%2C%22store%22%3A%22simple-auth-session-store%3Alocal-storage%22%7D%2C%22simple-auth-oauth2%22%3A%7B%22serverTokenEndpoint%22%3A%22https%3A//jsonstub.com/oauth/v2/token%22%2C%22serverTokenRevocationEndpoint%22%3A%22https%3A//jsonstub.com/oauth/v2/token/destroy%22%2C%22refreshAccessTokens%22%3Atrue%7D%2C%22contentSecurityPolicyHeader%22%3A%22Content-Security-Policy-Report-Only%22%2C%22contentSecurityPolicy%22%3A%7B%22default-src%22%3A%22%27none%27%22%2C%22script-src%22%3A%22%27self%27%22%2C%22font-src%22%3A%22%27self%27%22%2C%22connect-src%22%3A%22%27self%27%22%2C%22img-src%22%3A%22%27self%27%22%2C%22style-src%22%3A%22%27self%27%22%2C%22media-src%22%3A%22%27self%27%22%7D%2C%22exportApplicationGlobal%22%3Afalse%7D" />

        <link rel="stylesheet" href="assets/vendor-ec593555806ed9257ba3499907f73f05.css">
        <link rel="stylesheet" href="assets/jsonstub-ember-11fc6f9189ee15ceb15648c1eb7cfe98.css">
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-47943224-1', 'auto');
        </script>
    </head>
    <body>
        <script src="assets/vendor-8d0596b15526f36ae70009098f6d3f25.js"></script>
        <script src="assets/jsonstub-ember-d11e3620aa442db7b2ba53d465f4e89e.js"></script>
    </body>
</html>

2条回答
Luminary・发光体
2楼-- · 2019-09-07 09:20

Maybe it's a CORS problem? Can you have a look at the DevTools Console output?

The content of the HTML response could also be helpful...

查看更多
够拽才男人
3楼-- · 2019-09-07 09:24

It's because the header 'Content-Type': 'application/json' is not sent in HTTP request.

In axios, if the request body is empty, Content-Type would be removed from HTTP headers. Anyway, Content-Type is the type of request body, so this design makes sense. Refer to discussion in GitHub.

However, Jsonstub announced that the Content-Type: application/json header must always be present for any stubbed requests. That's why you get strange html response when header 'Content-Type': 'application/json' is missing.

The workaround is simple, pass arbitrary data in HTTP request body:

axios.get('http://jsonstub.com', {
  headers: {
    'Content-Type': 'application/json',
    'JsonStub-User-Key': '1699b7fc-cdcb-4205-b30a-b9b3626246c5',
    'JsonStub-Project-Key': 'f39f1bd2-d303-4b7e-9a11-c0f049a46f87'
  },
  data: {}
}).then((data) => console.log(data))
查看更多
登录 后发表回答