Skip to content

Having problems deleting file via REST api - getting 404 #1765

Closed

Description

@astanton

I have some cloud code that I'm calling from my parse instance that is the following:

Parse.Cloud.httpRequest({
            method: 'DELETE',
            url: url,
            headers: {
                'X-Parse-Application-Id': process.env.APP_ID,
                'X-Parse-Master-Key': process.env.MASTER_KEY
            }
        }).then(function(httpResponse) {
            console.log(httpResponse.text);
        }, function(httpResponse) {
            console.error('Request failed with response code ' + httpResponse.status);
        })

I'm getting the message Request failed with response code 404 in the heroku log. I'm positive that the url exists that I'm passing in because I can hit it in my browser and it downloads the image that I am trying to delete.

I'm also positive that my process.env.APP_ID and process.env.MASTER_KEY are correct as well since I've printed those to the log as well and seen them.

Is there another step that I'm missing here?

Activity

  1. astanton commented on May 13, 2016

    @astanton
    Author

    Just for reference, this is what I was trying to follow to delete the files.

    https://parse.com/questions/how-can-i-delete-a-file

  2. drew-gross commented on May 13, 2016

    @drew-gross
    Contributor

    Could you post some parse server logs as well? Also, was this file originally created on api.parse.com? If so, you'll need to use api.parse.com to delete it.

  3. astanton commented on May 13, 2016

    @astanton
    Author

    @drew-gross No these were created with the server hosted on Heroku.

    Here is the log of a delete request that fails:

    2016-05-13T20:14:43.495784+00:00 heroku[router]: at=info method=DELETE path="/parse/files/SBcAs3A0hkTsaoU5JW7GP4YlRVNfMNo7tRg1rQJy/774d87665ad847441f9b31bd7b0ea9a3_file.bin" host=app-name.herokuapp.com request_id=3f12a88e-6857-4cc0-9c6e-b889bdfd7f0b fwd="54.166.161.252" dyno=web.1 connect=0ms service=4ms status=404 bytes=626

    And in this case, the url that was passed into the Parse.Cloud.httpRequest call as shown in my original post was:

    http://app-name.herokuapp.com/parse/files/SBcAs3A0hkTsaoU5JW7GP4YlRVNfMNo7tRg1rQJy/774d87665ad847441f9b31bd7b0ea9a3_file.bin

    And if I hit that URL directly, it downloads the picture to my laptop.

  4. drew-gross commented on May 13, 2016

    @drew-gross
    Contributor

    Thats a Heroku log. I need the Parse Server log. Set verbose: true in your server options to enable logging.

  5. astanton commented on May 13, 2016

    @astanton
    Author

    @drew-gross would that be in the Heroku Config Variables that I set that? Or in my index.js file where I'm calling new ParseServer() and pass that to it in the config object?

  6. drew-gross commented on May 13, 2016

    @drew-gross
    Contributor

    In your index.js where you call new ParseServer().

  7. astanton commented on May 13, 2016

    @astanton
    Author

    @drew-gross Sorry for the noobness, but where will these logs appear after I add that flag? I assumed they would add more stuff to the application server log that I see on Heroku but nothing more is showing up there.

  8. drew-gross commented on May 13, 2016

    @drew-gross
    Contributor

    That flag will cause every request and response to be logged. If there is nothing showing up, then there must be some other issue with your configuration.

  9. astanton commented on May 14, 2016

    @astanton
    Author

    @drew-gross any idea how I can figure out what else may be wrong with the configuration? Here's the call that creates the new server object.

    var api = new ParseServer({
        databaseURI: databaseUri,
        cloud: __dirname + '/cloud/index.js',
        appId: appId,
        masterKey: masterKey,
        serverURL: serverURL,
        facebookAppIds: ['1234567890'],
        verbose: true,
        push: {
            ios: {
                pfx: __dirname + '/certs/cert.p12',
                bundleId: 'bundle.id',
                production: false
            }
        }
    });
    
  10. astanton commented on May 19, 2016

    @astanton
    Author

    @drew-gross okay so I figured out the issue with verbose and I now have it working.

    When I call the code in question, I don't even see the DELETE request in the parse-server logs at all.

    I have this running locally and here is the exact code I'm running:

    Parse.Cloud.define('removeOldFilesFromDB', function (request, response) {
    
        var fileUrls = request.params.fileUrls;
        var promises = [];
    
        for (var a = 0; a < fileUrls.length; a++) {
    
            promises.push(Parse.Cloud.httpRequest({
                method: 'DELETE',
                url: fileUrls[a],
                headers: {
                    'X-Parse-Application-Id': process.env.APP_ID || 'myAppId',
                    'X-Parse-Master-Key': process.env.MASTER_KEY || 'myMasterKey'
                }
            }));
    
        }
    
        Parse.Promise.when(promises).then( function (result) {
            console.log("Successfully deleted files");
            response.success(result);
        }, function(a,b,c,d,e) {
            console.log("Nope jacked it up!");
            response.error("Error deleting files");
        });
    
    })
    

    When I put a break point down in the error function, "a" is just an array and they all say error 404.

    Also, this is the exact output after I make that call, on the log:

    verbose: POST /parse/functions/removeOldFilesFromDB { host: 'e0ae0682.ngrok.io',
      'x-parse-client-version': 'i1.12.0',
      accept: '*/*',
      'x-parse-session-token': 'r:5290c76c47678213770765d990ee38b6',
      'x-parse-application-id': 'myAppId',
      'x-parse-client-key': 'myClientKey',
      'x-parse-installation-id': '3fafc219-1c63-4d68-b42f-5caa3e1c27cb',
      'accept-language': 'en-us',
      'accept-encoding': 'gzip, deflate',
      'x-parse-os-version': '8.1 (12B411)',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '204',
      'user-agent': 'Spin%20the%20Bottle/1 CFNetwork/711.1.12 Darwin/14.0.0',
      'x-parse-app-build-version': '1',
      'x-parse-app-display-version': '1.0',
      'x-forwarded-for': '71.163.238.223' } {
      "fileUrls": [
        "http://e0ae0682.ngrok.io/parse/files/myAppId/7b5b50e2871af27971be0425f367ab96_file.bin",
        "http://e0ae0682.ngrok.io/parse/files/myAppId/cff909602b60b15331b1ac60a4f08697_file.bin"
      ]
    }
    At top of remove old files func file urls length is 2 and values are ["http://e0ae0682.ngrok.io/parse/files/myAppId/7b5b50e2871af27971be0425f367ab96_file.bin","http://e0ae0682.ngrok.io/parse/files/myAppId/cff909602b60b15331b1ac60a4f08697_file.bin"]
    Nope jacked it up!
    verbose: error: code=141, message=Error deleting files
    

    So it's odd that the DELETE call is not even showing up in there. Am I calling it wrong with the Parse.Cloud.httpRequest method or something?

    And again, if I hit the values for fileUrl in my browser, it downloads the images I'm trying to delete so the path is correct.

    This is on the latest released version of parse-server, 2.2.10.

  11. drew-gross commented on May 19, 2016

    @drew-gross
    Contributor

    That is not the correct way to delete files. Please check the docs or ask a question on Stack Overflow if you are having trouble, this repo is for reporting bugs in Parse Server.

  12. astanton commented on May 19, 2016

    @astanton
    Author

    Well I was following this here:

    https://parse.com/docs/rest/guide/#files-deleting-files

    And this here to make the rest call from the cloud code:

    https://parse.com/questions/making-a-rest-call-from-within-cloud-code

    According to that, it says I can use Parse.Cloud.httpRequest to make REST calls, so I am not sure what you are referring to as that not the correct ways. Both of those have links to the docs and that is what I followed.

  13. ZhenanH commented on May 21, 2017

    @ZhenanH

    @astanton have you solved the problem, I have similar problem. I used Parse.Cloud.httpRequest to make REST calls to delete photos, but always fails.

  14. junal commented on Oct 5, 2017

    @junal

    @astanton how did you fix this problem?

  15. iatek commented on Mar 8, 2018

    @iatek

    At long last, I determined why the endpoint was not working, and always returning a 404. The name of the request URL is very important! It's NOT the full file url as one would expect. For example, the full url of the files on my parse-server look like:

    http://myserver.com/parse/files/MqX4RIjBpnVgUg9lRrxmWRsDVmqR5SCOaYmnSpnx/fa4492b3be10b5eac60881d2c6462730.png

    To get the /files endpoint to DELETE this file the request URL is:

    http://site2img-api.herokuapp.com/parse/files/fa4492b3be10b5eac60881d2c6462730.png

    So, when the docs mentioned "To delete a file, send a DELETE request to the files URL, postfixed with the name of the file", it's correct! You just need to get rid of that extra /xxxxxxxxx before the actual filename.

    Here's my working cloud code....

    Parse.Cloud.httpRequest({
    	method: "DELETE",
    	url: "http://.../parse/files/664a8e00a0ccd0fd44ca1d4e4a0c2ca5.png",
    	headers: {
    		'X-Parse-Application-Id':'MqX4RIjBpnVgUg9lRrxmWRsDVmqR5SCOaYmnSpnx',
    		'X-Parse-Master-Key':'ZjpLGVWBJujDdElxxGeEh2fpS041PiQSAqk8cwCi',
    	}
    }).then(function(httpResponse) {
       //... it worked!!
    }, function(httpResponse) {
    	console.error('Delete request failed with response code ' + JSON.stringify(httpResponse));
    });
    
  16. JacobJT commented on Mar 9, 2018

    @JacobJT

    I think it may also have had to do with you using ngrok in the request instead of your actual server URL.

  17. emadehsan commented on Apr 26, 2018

    @emadehsan
    Contributor

    @iatek you are right! remove the Application Id from file URL
    If file is at: https://YOUR.PARSE-SERVER.HERE/parse/files/SOME_APP_ID/profile.png, you need to make request at https://YOUR.PARSE-SERVER.HERE/parse/files/profile.png

    curl -X DELETE \
      -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
      -H "X-Parse-Master-Key: ${MASTER_KEY}" \
      https://YOUR.PARSE-SERVER.HERE/parse/files/profile.png
    

    Docs don't explicitly specify this

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