Skip to content

After upgrade to 1.0 GET request with parameters in the url fail #4999

Closed
javascript-indonesias/axios
#146

Description

@jweingarten

Working request with Axios v1.0.0-alpha.1:

axios.get('https://git.somegit.com/api/v3/search/commits?q=sort:committer-date-desc merge:false repo:org/reponame&per_page=50&page=1

Same request with 1.0.0 fails with a 404. 1.0.0 was promoted today.

When I print the failed response it looks like something is parsed/encoded incorrectly.

   request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
       .....
       ..... 
      _headerSent: true,
      socket: [TLSSocket],
      _header: 'GET /api/v3/search/commitsq=sort%3Acommitter-date-desc+merge%3Afalse+repo%3Aorg%2Freponame&per_page=50&page=1 HTTP/1.1\r\n' +
        'Accept: application/vnd.github.cloak-preview+json;application/vnd.github.v3+json\r\n' +
        'User-Agent: axios/1.0.0\r\n' +
        'Accept-Encoding: gzip, deflate, br\r\n' +
        'Host: git.somegit.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',

To me the ? is missing in between commits and q.

When I don't parse the parameters on the URL and use config.params it works with Axios 1.0.0.

    config.params = {
        q: `sort:committer-date-desc merge:false repo:org/reponame`,
        per_page: 50,
        page: 1
    }

The success response then shows this _header:

 _header: 'GET /api/v3/search/commits?q=sort:committer-date-desc+merge:false+repo:org%2Freponame&per_page=50&page=1 HTTP/1.1\r\n' +
      'Accept: application/vnd.github.cloak-preview+json;application/vnd.github.v3+json\r\n' +
      'User-Agent: axios/1.0.0\r\n' +
      'Accept-Encoding: gzip, deflate, br\r\n' +
      'Host: git.somegit.com\r\n' +
      'Connection: close\r\n' +
      '\r\n',

In the working version I do see the ?.

So right now the workaround is either sticking with v1.0.0-alpha.1 or using config.params instead of passing them with the .get call.

Activity

  1. arthurfiorette commented on Oct 5, 2022

    @arthurfiorette
    Contributor

    Weird...
    Can you provide a reproducible repository so it can be tested out more quickly?

  2. jweingarten commented on Oct 5, 2022

    @jweingarten
    Author

    Not really ... sorry. I can't give you access to our GIT server to make that GIT api call. I am not sure if its a generic thing with any parameters in the url or just this specific set.
    Anything I can provide you with as far as debug print outs where I can remove internal data?

  3. arthurfiorette commented on Oct 5, 2022

    @arthurfiorette
    Contributor

    No need for git acces, just a 2 files repository on github with a nodejs script that calls an express server that throws the same error. Because the above information doesn't seem enough to find a precise solution.

  4. jweingarten commented on Oct 5, 2022

    @jweingarten
    Author

    How about this?

    Curl command for illustration only

    For illustration ... a curl command that returns with my personal GIT Access token a list of commits when searching the axios repo. As I said its just for illustration. If you want to execute the curl command you need to update the my_token value in the command.

    Request:

    curl -v -s -X GET -H "Accept: application/vnd.github.v3+json" -H "Authorization:token my_token" https://api.github.com/search/commits?q=sort:committer-date-desc+merge:false+repo:axios/axios&per_page=50&page=1
    

    Response looks normal as I get a list of commits.

    Axios v1.0.0-alpha.1

    Steps/Run

    • rm -rf node_modules
    • npm install [email protected]
    • vi test.js
    • copy/paste this into test.js
    const axios = require('axios');
    
    const config = {
        headers: {
            Authorization: 'token my_token',
            Accept: 'application/vnd.github.v3+json'
        }
    }
    
    axios.get('https://api.github.com/search/commits?q=sort:committer-date-desc+merge:false+repo:axios/axios&per_page=50&page=1', config).then(function (response) {
        // handle success
        console.log(response);
    })
        .catch(function (error) {
            // handle error
            console.log(error);
        })
    
    • Update my_token with your GIT Access token in above code and save.
    • node test.js

    Response looks normal as I get an array of commits back.

    Axios v1.0.0

    Steps/Run

    • rm -rf node_modules
    • npm install axios
    • use same test.js as above
    • node test.js

    Response

    AxiosError: Request failed with status code 404
    ...
    ...
    ...
      data: {
          message: 'Not Found',
          documentation_url: 'https://docs.github.com/rest'
        }
    
  5. jweingarten commented on Oct 5, 2022

    @jweingarten
    Author

    Forgot to mention ... I am testing with node --version v14.18.2

  6. ScriptCamilo commented on Oct 5, 2022

    @ScriptCamilo

    I am also having the same or similar issue, I was just studying and when I installed the new upgraded version my get call was returning a 404 not found status. I'm using node --version v16.17.0. My study code was something like this:

    const { get } = require('axios');
    const URL = 'https://swapi.dev/api/people';
    
    async function getPeople(name) {
      const url = `${URL}/?search=${name}&format=json`;
      console.time('call-api');
      const response = await get(url);
      console.timeEnd('call-api');
    
      console.log(response.data.results);
      return response.data;
    }
    
    getPeople('r2-d2');
    
    module.exports = {
      getPeople,
    };
    

    It works fine on the "axios": "^0.27.2"

  7. jasonsaayman commented on Oct 5, 2022

    @jasonsaayman
    Member

    This might be an oversight but in v1 we have been a lot stricter with our URL parsing. The URL is for your URL free of any query params. Thus we have params as part of the config.

    If you use a URL like this http://test.example.com?test=1 we will be stripping out the ?. Please use the params to set query params.

  8. evanhurd commented on Oct 5, 2022

    @evanhurd

    That's ridiculous. I love axios and it's my goto client in node but this breaking change hurts the ergonomics of the library.

  9. janitza-mage commented on Oct 5, 2022

    @janitza-mage

    Please use the params to set query params.

    I have the same problem as the original poster.

    Does this mean that, to send a request to an URL from elsewhere that may contain a query string, I have to detect the question mark, split the URL manually and put the querystring into the params? This probably also means that I should be using URLSearchParams to avoid writing a querystring parser myself, right?

  10. ddolcimascolo commented on Oct 5, 2022

    @ddolcimascolo

    Same here, we're going to refactor but there is no mention of this in the breaking changes

  11. ddolcimascolo commented on Oct 5, 2022

    @ddolcimascolo

    Fun fact, axios.getUri still work... This honestly look like ther's something weird.

    Can we get a word from Axios maintainers ?

  12. ddolcimascolo commented on Oct 5, 2022

    @ddolcimascolo

    For those wondering, or too lazy to refactor everything, or using Axios to fetch full URLs they don't manage (our use case: we fetch a RSS feed containing links to items and the item URL might contain query params, it's an opaque string for us) I resorted to using this interceptor:

    instance.interceptors.request.use((req) => {
      const url = new URL(axios.getUri(req)), // getUri will build the final URI, merging defaults
            params = new URLSearchParams(url.searchParams);
    
      url.search = '';
    
      req.url = url.href; // The URL without query params
      req.params = params; // The merged query params from defaults + request
    
      return req;
    });

    In my context, I'm paying the extra CPU cycles for the useless interceptor. I'm hoping for Axios to reintroduce previous behavior

  13. abrkn commented on Oct 5, 2022

    @abrkn

    Thanks, @ddolcimascolo. Is the order of the params deterministic? I'm using axios for an API that signs the search params and appends that as the last search param:

    GET /some-api/secret?f1=a&f2=b&f3=c&signature=<sig>

    Where <sig> is determined by signing GET/some-api/secret?f1=a&f2=b&f3=c

  14. abrkn commented on Oct 5, 2022

    @abrkn

    This is a very unfortunate breaking change which I hope the maintainers will reconsider.

  15. ddolcimascolo commented on Oct 5, 2022

    @ddolcimascolo

    Thanks, @ddolcimascolo. Is the order of the params deterministic? I'm using axios for an API that signs the search params and appends that as the last search param:

    GET /some-api/secret?f1=a&f2=b&f3=c&signature=<sig>

    Where <sig> is determined by signing GET/some-api/secret?f1=a&f2=b&f3=c

    I'd say yes

  16. 16 remaining items

  17. added a commit that references this issue on Oct 5, 2022
    3e4d521
  18. jasonsaayman commented on Oct 5, 2022

    @jasonsaayman
    Member

    Fixed with #5018

  19. jasonsaayman commented on Oct 5, 2022

    @jasonsaayman
    Member

    Sorry about that, something that I should have picked up, I will try to be a bit more strict on changes which are to largely incompatible.

  20. ddolcimascolo commented on Oct 5, 2022

    @ddolcimascolo

    Thx for fixing @jasonsaayman 👍

  21. jasonsaayman commented on Oct 5, 2022

    @jasonsaayman
    Member

    No probs but credit goes to @DigitalBrainJS. Sorry for all the problems this caused. Please can anyone interested watch our releases as we need many more testers on pre-releases, I can only do so much.

  22. added a commit that references this issue on Oct 6, 2022
  23. gdelory commented on Oct 6, 2022

    @gdelory

    Any idea on the date of the next release? Just to know if we rollback to 0.x or wait for the new release :)

  24. jasonsaayman commented on Oct 6, 2022

    @jasonsaayman
    Member

    Releaseing now

  25. philBrown commented on Oct 6, 2022

    @philBrown

    @jasonsaayman Why was the fix shipped in a minor release and not a patch? I would have expected v1.0.1

    Won't this remain broken for anyone using approximate equivalent version?

    "axios": "~1.0.0"
  26. abrkn commented on Oct 7, 2022

    @abrkn

    Thanks for fixing this issue so quickly, @jasonsaayman!

  27. janitza-mage commented on Oct 7, 2022

    @janitza-mage

    Can confirm that it works for me. That was fast! Thanks a lot.

  28. jasonsaayman commented on Oct 7, 2022

    @jasonsaayman
    Member

    Im bumping as minor cause its not the only change

  29. philBrown commented on Oct 7, 2022

    @philBrown

    @jasonsaayman could you please roll out a v1.0.1 patch release with the URL query param fix then?

    That or pull v1.0.0 entirely. As mentioned already, anyone using ~1.0.0 won't get the fix

  30. mgiraldo commented on Mar 29, 2023

    @mgiraldo

    had this same issue. reverting to 0.27.2 fixed it.

    this is how a working call looks for me (v0.27.2):

    image

    this is what a broken call looks for me (v1.3.4) with no code changes, just upgrading the package:

    image

  31. rangelanino commented on Mar 30, 2023

    @rangelanino

    Same issue. Though I think its partly because our app uses WebPack and not Rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions