Skip to content

Missing request token for request, while uploading image file getting error. #29021

Closed

Description

@sahilpandya

Missing request token for request, while uploading image file getting error.

Description

While image is uploading to server, sometime its throwing error Missing request token.

React Native version: v0.61.5

Run react-native info in your terminal and copy the results here.

System:
OS: macOS Mojave 10.14.6
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Memory: 147.43 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v10.16.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.0, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
IDEs:
Xcode: 11.0/11A420a - /usr/bin/xcodebuild
npmPackages:
react: 16.12.0 => 16.12.0
react-native: 0.61.5 => 0.61.5
npmGlobalPackages:
react-native-cli: 2.0.1

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Add an image from camera and add an image from library.
  2. then upload images to server.

Expected Results

Images should be uploaded successfully.

Snack, code example, screenshot, or link to a repository:

IMG_613148776FD6-1

Activity

  1. khuyendev commented on Jun 4, 2020

    @khuyendev

    same issue here

  2. MaxEgg commented on Jun 13, 2020

    @MaxEgg

    Having this issue constantly in version: 0.63.0-rc.0. Can not upload a file. Tried using the fetch api and Axios. Both throw this error when adding formData with image data to the request. Adding formData with a string value still works as expected.

  3. linoleum00 commented on Jun 25, 2020

    @linoleum00

    I'm using RN 0.62.2, and I'm having this issue. I'm using rxjs/ajax to upload to a multipart POST API and it crashes!

  4. sahilpandya commented on Jun 29, 2020

    @sahilpandya
    Author

    I got one work around, adding setTimeout for each request, So now am able to upload 90% times. But it is still not 100% workaround.

    here is a sample code:
    return new Promise(function(resolve, reject) { setTimeout(() => { return request.put(file, options) .then(response => { // success resolve() }) .catch((err) => { reject(err); }); }, 1500); }) .catch(error => { // error })

  5. linoleum00 commented on Jun 29, 2020

    @linoleum00

    I also read this same workaround, and as you said it is not a 100% solution, sometimes it still crashes

  6. HugoGresse commented on Jul 9, 2020

    @HugoGresse

    I've updated the app to RN 0.63 and reproduce this 100% o the time, either on emulator or iPhone...
    To be more clear, the image doesn't even go out of the device, using Charles proxy doesn't show any network usage upon upload.

  7. wezzy commented on Jul 9, 2020

    @wezzy

    Same here, after the upgrade if I make a request using fetch("SOME LOCAL PATH") I get the error. It's woking correctly with the older version

  8. kelset commented on Jul 10, 2020

    @kelset
    Contributor

    Since this seems to be surfacing more with the new 0.63, can someone who is facing this issue please post a full repro? Meaning, a sample react-native init'd project with the minimal code to reproduce the issue posted above.

  9. v-anton commented on Jul 13, 2020

    @v-anton

    100% reproducible on 0.63 (iOS).

  10. dharminderraja commented on Jul 13, 2020

    @dharminderraja

    same here i am using react-native-aws3 to upload the image and it always have this error.
    this error start happening when i updated the rn to 0.63 was working fine in previous version of rn.

  11. struct78 commented on Jul 13, 2020

    @struct78

    Have tested this using fetch to read an image with the file protocol and get the same error. Replacing the file:/// URL with a https:// URL works fine.

    I don't have time to put it in an easily reproducible demo unfortunately.

    https://github.com/trueme-app/trueme-react-native/blob/develop/src/services/s3.ts#L36

    Having dug a bit deeper (and not fully understanding how RN workers under the hood, so I apologise in advance if I'm wrong!), I wonder if this commit is the culprit? 058eeb4 @p-sun

  12. leops commented on Jul 13, 2020

    @leops

    I stumbled upon this problem too, quickly looking at it it seems to me the origin for the problem is this function:

    https://github.com/facebook/react-native/blob/f898bb65fac3f26944cbe1c47b87c63b2cd10e03/Libraries/Image/RCTImageLoader.mm#L1101-L1131

    When loading a file from the local filesystem loadImageWithURLRequest can (and does) load the file synchronously, and calls the callback block immediately. Since the function hasn't returned yet requestToken is not initialized, hence the "Missing request token" error

  13. v-anton commented on Jul 13, 2020

    @v-anton

    I stumbled upon this problem too, quickly looking at it it seems to me the origin for the problem is this function:

    https://github.com/facebook/react-native/blob/f898bb65fac3f26944cbe1c47b87c63b2cd10e03/Libraries/Image/RCTImageLoader.mm#L1101-L1131

    When loading a file from the local filesystem loadImageWithURLRequest can (and does) load the file synchronously, and calls the callback block immediately. Since the function hasn't returned yet requestToken is not initialized, hence the "Missing request token" error

    Hey @leops
    Please let me know if you need any help. Looking forward to a solution, but can't manage it due to another work tasks :(

  14. 63 remaining items

  15. eco-line commented on Sep 30, 2020

    @eco-line

    This got fixed in v0.63.3

  16. cherucole commented on Oct 9, 2020

    @cherucole

    This got fixed in v0.63.3

    I upgraded RN to this version and never saw the issue again

  17. safaiyeh commented on Oct 9, 2020

    @safaiyeh
    Contributor

    Thanks everyone for your patiences. Glad the fix is in, closing

  18. thaiphamvan commented on Oct 23, 2020

    @thaiphamvan

    dataForm.append('file', { uri: Platform.OS=='ios'?photo.uri.replace("file://", "/private"):photo.uri, name: 'photo.jpg', type: 'image/jpg' });

    IOS : photo.uri.replace("file://", "/private")

    thanks , It works for me.

  19. Balasnest commented on Nov 3, 2020

    @Balasnest

    getting this npm error after change package json from 0.63.2 to 0.63.3.

    npm ERR! code EINTEGRITY
    npm ERR! Verification failed while extracting jsc-android@241213.1.0:
    npm ERR! Verification failed while extracting jsc-android@241213.1.0:
    npm ERR! sha512-AH8NYyMNLNhcUEF97QbMxPNLNW+oiSBlvm1rsMNzgJ1d5TQzdh/AOJGsxeeESp3m9YIWGLCgUvGTVoVLs0p68A== integrity checksum failed when using sha512: wanted sha512-AH8NYyMNLNhcUEF97QbMxPNLNW+oiSBlvm1rsMNzgJ1d5TQzdh/AOJGsxeeESp3m9YIWGLCgUvGTVoVLs0p68A== but got sha512-ptI+efRlVdodigsznoyx5zuvQcHCk3NgWkC8GR/qr2pRl87wCpMDqzDs/zvKfm3zkBALvOaF2E/gD5DePG4G4Q==. (4273248 bytes)

    Any idea?

  20. Leehee1on commented on Dec 11, 2020

    @Leehee1on

    node_modules/react-native/Network/NetworkTesk.mm

    Line 102 ~ 107

    if (requestToken == nil) {
    if (RCT_DEBUG) {
    // RCTLogError(@"Missing request token for request: %@", _request); // change
    }
    valid = YES; // change NO -> YES
    }

    My solution is RN < 63.2

  21. GuilhermeTrivilin commented on Jan 5, 2021

    @GuilhermeTrivilin

    Error fixed when updating React Native to version 0.63.3

  22. tbsphpdev commented on Jan 12, 2021

    @tbsphpdev

    This working for me
    data.append('image',
    {
    uri: Platform.OS === 'android' ? photo.uri : photo.uri.replace('file://', '/private'),
    name: 'image.jpg',
    type: 'image/jpg',
    });

  23. Mahmud-cdx commented on Mar 6, 2021

    @Mahmud-cdx

    I also faced same issue. But I manually updated the RCTNetworkTask.mm file. I know it is not correct method but at least I can upload the image now until React-Native developer share their fix to us.

    node_modules/react-native/Libraries/Network/RCTNetworkTask.mm

    - (BOOL)validateRequestToken:(id)requestToken
    {
      BOOL valid = YES;
      if (_requestToken == nil) {
        if (requestToken == nil) {
          if (RCT_DEBUG) {
    //        RCTLogError(@"Missing request token for request: %@", _request);
          }
          valid = YES;
        }
        _requestToken = requestToken;
      } else if (![requestToken isEqual:_requestToken]) {
        if (RCT_DEBUG) {
          RCTLogError(@"Unrecognized request token: %@ expected: %@", requestToken, _requestToken);
        }
        valid = NO;
      }
    
      if (!valid) {
        _status = RCTNetworkTaskFinished;
        if (_completionBlock) {
          RCTURLRequestCompletionBlock completionBlock = _completionBlock;
          [self dispatchCallback:^{
            completionBlock(self->_response, nil, RCTErrorWithMessage(@"Invalid request token."));
          }];
        }
        [self invalidate];
      }
    
      return valid;
    }
    

    thank you very much. It is working

  24. michaelVictoriaDev commented on Mar 17, 2021

    @michaelVictoriaDev

    still get error in 0.63.4

  25. victorglomer commented on Mar 27, 2021

    @victorglomer

    In my case, thats what worked

        const data = new FormData();
        data.append("avatar", {
          type: "image/jpeg",
          name: `${user.id}.jpg`,
          uri:
            Platform.OS == "ios"
              ? response.uri?.replace("file://", "/")
              : response.uri,
        });
    
  26. rdewolff commented on May 15, 2021

    @rdewolff

    In my case, thats what worked

        const data = new FormData();
        data.append("avatar", {
          type: "image/jpeg",
          name: `${user.id}.jpg`,
          uri:
            Platform.OS == "ios"
              ? response.uri?.replace("file://", "/")
              : response.uri,
        });
    

    This worked for me too! Thanks for sharing.

  27. locked as resolved and limited conversation to collaborators on Oct 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions