question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behavior when set baseUrl and use Post method

See original GitHub issue

Hi, I encountered with problem when do post request. If I use this approach, all work good

import axios from 'axios'

var loginApiService = {
    getToken() {
        axios.post("/token", { email: "test@email.ru", password: "pass" })
            .then((response) => {
                return response.data;
            });
}

Request : POST http://localhost:59288/token HTTP/1.1 Host: localhost:59288 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0 Accept: application/json, text/plain, / Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Referer: http://localhost:59288/login?redirect=%2F Content-Type: application/json;charset=utf-8 Content-Length: 43 Cookie: Connection: keep-alive Pragma: no-cache Cache-Control: no-cache

{“email”:“test@email.ru”,“password”:“pass”}

But if I set base url :

import axios from ‘axios’

var loginApiService = {
    getToken() {

        axios({
            method: 'post',
            url: 'http://localhost:33333/token',
            data: {
                email: "test@email.ru",
                password:
                    "pass"
            }
        });
}

I have request:

OPTIONS http://localhost:33333/token HTTP/1.1 Host: localhost:33333 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type Origin: http://localhost:59288 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache

I did not see data in body and get 404 from server.

The same problem if I use

import axios from 'axios'
var apiClient = function () {
    return axios.create({
        baseURL: 'http://localhost:33333/',
        headers: {
            'Content-Type': 'application/json',
        }
    });
}

If I set Content-Type to application/x-www-form-urlencoded, it works, but I can obtain data only from FromForm. But I want to work with body.

Context

axios : 0.18v firefox: 61.0b5

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
dreamevilgfcommented, Jun 4, 2018

I found out reason of this behavior. I didn’t allow CORS requests on server side.

I added cors policy and now axios works fine!

0reactions
savioserracommented, May 25, 2018

Seems like your endpoint is on port 59288 in the first example. In the second one, you explicitly changed the port to 33333, and got a 404 (not found, which seems right to me).

I would recommend you to make both requests via curl and share the results with us.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing axios results in a casing unexpected behaviour ...
When I use the imported object, it does not take the baseUrl from the instance I created. MyAxios.post('/orders.json', order) .then(response ...
Read more >
Communicating with backend services using HTTP - Angular
Most front-end applications need to communicate with a server over the HTTP protocol, in order to download or upload data and access other...
Read more >
TSConfig Reference - Docs on every TSConfig option
The path may use Node.js style resolution. The configuration from the base file are loaded first, then overridden by those in the inheriting...
Read more >
Upcoming deprecation of baseURL in Ember CLI 2.7
This implicit behavior violated the principle of least surprise for many people. An explicit requirement for both values always being set and ...
Read more >
Base requests - Community Powered Vulnerability Scanner
Request method can be GET, POST, PUT, DELETE, etc. depending on the needs. ... requests: - method: GET path: - "{{BaseURL}}/login.php" redirects: true ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found