Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get request with empty object as data does nothing on IOS simulator #2380

Closed
bondansebastian opened this issue Aug 28, 2019 · 2 comments
Closed

Comments

@bondansebastian
Copy link

bondansebastian commented Aug 28, 2019

Bug Description
Get request with empty object as data does nothing on IOS simulator.
The promise never resolved, and no error thrown.

To Reproduce
I tried to send GET request using axios, using axios.request(options) method.
Here's the look of my options object

{ 
    data: {},
    ...
}

As can be seen, there's data with empty object.
If i don't remove that data, then axios just doesn't do anything.
There's no error, nor the promise ever resolved.

I haven't tried this on actual IOS device though.

Here's the wrapper function i use to make the request

async _request(request) {
    let options = {
      // `url` is the server URL that will be used for the request
      url: request.url,

      // `method` is the request method to be used when making the request
      method: request.method ? request.method : "get", // default

      // `baseURL` will be prepended to `url` unless `url` is absolute. It can be
      // convenient to set `baseURL` for an instance of axios to pass relative URLs to
      // methods of that instance.

      baseURL: this.BASE_URL,
      // baseURL: !identity ? loginServer : identityServer ,

      // `headers` are custom headers to be sent
      //headers: null,

      // `params` are the URL parameters to be sent with the request Must be a plain
      // object or a URLSearchParams object
      // params: null,

      // `data` is the data to be sent as the request body Only applicable for request
      // methods 'PUT', 'POST', and 'PATCH' When no `transformRequest` is set, must be
      // of one of the following types:
      // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
      // - Browser only: FormData, File, Blob
      // - Node only: Stream, Buffer
      //data: null,

      // `timeout` specifies the number of milliseconds before the request times out.
      // If the request takes longer than `timeout`, the request will be aborted.
      //timeout: 1000,

      // `withCredentials` indicates whether or not cross-site Access-Control requests
      // should be made using credentials
      // withCredentials: false, // default

      // `responseType` indicates the type of data that the server will respond with
      // options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
      // responseType: request.responseType ? request.responseType : 'json', // default

      // `validateStatus` defines whether to resolve or reject the promise for a given
      // HTTP response status code. If `validateStatus` returns `true` (or is set to
      // `null` or `undefined`), the promise will be resolved; otherwise, the promise
      // will be rejected.
      // validateStatus: function (status) {
      //     return status >= 200 && status < 400; // default
      // },

      cancelToken: new CancelToken(function(cancel) {
        // console.log("cancel",cancel)
      })
    };

    let token = await AsyncStorage.getItem(STORAGE.TOKEN_EXTRA);

    if (request.auth) {
      //if basic auth
      options["auth"] = request.auth;
    }

    if (token) {
      let parseToken = await JSON.parse(token);
      let Authorization = "Bearer " + parseToken;
      options["headers"] = { Authorization: Authorization };
    }

    // console.log(request.contentType)

    if (request.contentType) {
      if (request.contentType === contentType.URLENCODED) {
        options["headers"] = {
          "Content-Type": "application/x-www-form-urlencoded"
        };
      }
    }

    if (request.params) {
      options["params"] = request.params;
    }

    if (!isEmpty(request.data)) {
      options["data"] = request.data;
    }
    // console.log(options);

    let res = axios.request(options);
    return res
      .then(response => {
        return response.data;
      })
      .catch(error => {
        // console.tron.log(error);
        if (error.response) {
          // The request was made and the server responded with a status code
          // that falls out of the range of 2xx

          // console.log(error.response.data);
          console.log(error.response.status);
          console.log(error.response.headers);

          const { status } = error.response;

          if (status >= 200 && status <= 400) {
            // let message = error.response.data.error || error.response.data.messages || error.response.data.Message
            let message = error.response.data.messages;
            // console.tron.log('error.response');
            // console.tron.log(error.response.data);
            throw message;
          } else {
            // let message = error.response.data;
            // console.tron.log(error);
            let message = "Server Error";
            console.log("message", message);
            throw message;
          }

          // return error.response.data
        } else if (error.request) {
          // The request was made but no response was received
          // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
          // http.ClientRequest in node.js

          console.log("error.request", error.request);
          let message = "Server Error";
          // throw error.request
          throw message;
        } else {
          // Something happened in setting up the request that triggered an Error
          console.log("Error", error.message);
          throw error.message;
        }
      });
  }

Notice that i need to check if request.data is empty in order to not include an empty object into data

Expected behavior
Axios should resolve the promise or at least rise an error.

Environment:
I'm using Expo to develop native mobile app.
Here's my package.json

"dependencies": {
    "axios": "^0.19.0",
    "expo": "^33.0.0",
    "expo-blur": "~5.0.1",
    "expo-linear-gradient": "~5.0.1",
    "moment": "^2.24.0",
    "prop-types": "^15.7.2",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
    "react-native-gesture-handler": "~1.2.1",
    "react-native-loading-placeholder": "0.0.6",
    "react-native-navigation": "^2.22.1",
    "react-native-progress": "^3.6.0",
    "react-native-reanimated": "1.0.1",
    "react-native-render-html": "^4.1.2",
    "react-native-screens": "1.0.0-alpha.22",
    "react-native-snap-carousel": "^3.8.0",
    "react-native-web": "^0.11.4",
    "react-navigation": "^3.11.0",
    "react-navigation-tabs": "^2.2.0",
    "react-placeholder": "^3.0.2",
    "react-redux": "^7.1.0",
    "redux": "^4.0.1",
    "redux-define": "^1.1.1",
    "redux-enhancer-react-native-appstate": "^0.3.1",
    "redux-persist": "^5.10.0",
    "redux-thunk": "^2.3.0",
    "sentry-expo": "^1.13.0"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.1.1",
    "metro-react-native-babel-preset": "^0.55.0",
    "react-native-dotenv": "^0.2.0",
    "reactotron-react-native": "1.14.0",
    "reactotron-redux": "1.13.0"
  }
@ramyareye
Copy link

As I understood you cannot send data with get in iOS.
this made it work for me
use

 if (!isEmpty(request.data)) {
  if (options.options === 'get') {
    options["params"] = request.params;
  } else {
    options["data"] = request.data;
  }  
}

instead of

if (request.params) {
  options["params"] = request.params;
}

if (!isEmpty(request.data)) {
  options["data"] = request.data;
}

@jasonsaayman
Copy link
Member

Hi,

Thank you for submitting the above issue. It seems the issue has gone stale. Should you feel that this issue still exists please submit a new issue.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants