API Proxying vuejs - api

The code is on github
The following example API Proxying During development
gives an example of a proxyTable
// config/index.js
module.exports = {
// ...
dev: {
proxyTable: {
// proxy all requests starting with /api to jsonplaceholder
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
under which they say,
The above example will proxy the request /api/posts/1 to http://jsonplaceholder.typicode.com/posts/1.
but clearly posts/1 is missing from the example.
Or should the /posts/1 be in the proxyTable or in the .vue file using axios like this,
axios.get("api/posts/1")
so that a HTTP GET request which starts with api with proxy to http://jsonplaceholder.typicode.com and then append the rest of that url, which in this case is /posts/1 and so it actually proxies to http://jsonplaceholder.typicode.com/posts/1
Is this correct?
----------EDIT FROM HERE DOWN------------
In my own case
// config/index.js
module.exports = {
// ...
dev: {
proxyTable: {
// proxy all requests starting with /conn to http://localhost:8081
'/conn': {
target: 'http://localhost:8081',
changeOrigin: true,
pathRewrite: {
'^/conn': ''
}
}
}
}
}
and
axios.get("/conn/api.php?action=read")
should proxy to,
http://localhost:8081/api.php?action=read
But I get a console error,
GET http://localhost:8080/conn/api.php?action=read 504 (Gateway Timeout)
Thanks

Related

Backend api call not working in Vue(front)

My environment
front - windows, port 3000
backend - linux (ubuntu) in docker container, port 5000
Vue(front) tsconfig.json
export default defineConfig({
plugins: [vue(), vueJsx()],
resolve: {
alias: {
"#": fileURLToPath(new URL("./src", import.meta.url)),
},
},
server: {
host: true,
port: 3000,
proxy: {
"/api": {
target: "http://127.0.0.1:5000",
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
});
front api call code
const response = await axios.post("http://127.0.0.1:5000/users/login?email=" + code);
backend(Nestjs) - main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: 'http://localhost:3000',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
});
await app.listen(5000);
}
bootstrap();
I expect the api call to go to the backend, localhost:5000
but i get a 404 error
When I tried with postman, I got normal return, but when I sent the request using axios in Vue with the same request, I got 404 not found.
The most incomprehensible thing is that with the current settings, it was normally requested when developing in the past.
I don't know how to fix this
please help a lot
Repositories currently being edited: https://github.com/PracticeofEno/ft_transcendence
thanks
omg... I send POST message..
But api is GET Message

Why am I getting CORS Error even after enabling Proxy setting in VueJs while making POST request to mapmyindia apis endpoints?

Error ScreenShot
Post Request Code (Vuejs)
let PROXY = "http://localhost:8080/";
let URL = PROXY + "api/security/oauth/token?grant_type=client_credentials"
URL += `&client_id=${this.ID}`;
URL += `&client_secret=${this.SECRET}`;
try {
let res = await axios.post(URL, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
console.log(res);
}
catch(err) {
console.log(err);
}
Vue.config.js (Proxy Setting)
module.exports = {
devServer: {
proxy: {
'https://outpost.mapmyindia.com/': {
target: "http://localhost:8080/",
changeOrigin: true
}
}
},
transpileDependencies: [
'vuetify'
]
}
I was getting a CORS-related error initially so I used this proxy thing but even then I am getting this error. There are some questions posted on StackOverflow related to the proxy setting in vuejs to remove CORS errors but none of them could resolve this issue therefore I have posted this question.
This is not a CORS issue. The ressource hasn't been found. You have 404 status code.
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

CORS issue with Vue js

I used vuejs, with address http://localhost:8000.
When calling api, I get CORS error. Sid not succeed request headers
request
Domain from origin http://localhost:8080 has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
let headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': true,
};
You need to create a "vue.config.js" with proxy configuration.
Example :
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8080',
ws: true,
changeOrigin: true
}
}
}
}
Three Options
Option 1. Edit Vue.config.js
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8081',
ws: true,
changeOrigin: true
}
}
}
}
Option 2 (Temporary/Test use case)
Use Cors Plugin in chrome Eg- Moseif CORS
Degrade your chrome version less than 71
Use chromium or Canary Browser.
Option 3
Look into your server Cors allowed or not you can add plugin for python cors and annotation for java on server to allow this call.

How can I use proxyTable to make cross origin request in vue-cli?

I want to use proxyTable with axios in my vue-cli project.
In my config/index.js, I put code like this:
proxyTable: {
'/getnews': {
target: 'https://xx.xxx.xx.x'
changeOrigin: true,
secure: false,
pathRewrite: {
'^/getnews': '/getnews'
}
}
}
and in my request function, it goes like this:
var url = '/getnews';
this.$axios({
methods: 'get',
url: url,
})
.then(response => {
console.log(response);
})
Now here comes the question, my browser console tells me that
xhr.js?ec6c:178 GET http://localhost:8080/getnews 504 (Gateway Timeout)
and the terminal says:
Error occurred while trying to proxy request /getnews from localhost:8080 to https://xx.xxx.xx.x (ECONNREFUSED)
so it looks like the proxy doesn't work out, my request still goes to my localhost. Anyone knows how to fix that?
I finally figure out with the help of my friend.
It's the lack of port number that causing the problem. The code should be like this:
proxyTable: {
'/getnews': {
target: 'https://xx.xxx.xx.x:8080'
changeOrigin: true,
secure: false,
pathRewrite: {
'^/getnews': '/getnews'
}
}
}
Then it works just fine.

Vue devServer proxy is not helping, I still get CORS error

I'm using #vue/cli 3.x and in my vue.config.js I have this:
devServer: {
proxy: {
"/api": {
ws: true,
changeOrigin: true,
target: "http://localhost:8080"
}
}
}
But I keep getting CORS error:
Access to XMLHttpRequest at 'http://localhost:8080/api' from origin
'http://localhost:3000' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested
resource.
Any idea?
It looks like the problem was with the axios configurations.
I had this definition: axios.defaults.baseURL = "http://localhost:8080/api";
I changed it to axios.defaults.baseURL = "api";
and it works.
module.exports = {
...
devServer: {
proxy: {
"^/api": {
target: url,
ws: true,
changeOrigin: true
}
}
},
}