crisp-im / node-crisp-api

:zap: Crisp API Node Wrapper
https://docs.crisp.chat/guides/rest-api/
MIT License
99 stars 39 forks source link

How to upload file via HTTP PUT w/? #44

Closed ehsan-mohammadi closed 2 years ago

ehsan-mohammadi commented 2 years ago

Hello again to the Crisp team! In the last week, I tried to use the upload feature. I could generate the signed URL, but my main problem is that I can't upload my file via HTTP PUT w/. I also tried to upload my file with Postman. I got 200 OK response, but nothing happened and my file didn't get uploaded.

Here is a screenshot of what I do in the Postman: image

I double-checked that the name and type of my file are the same as the name and type that I used for generating the signed URL. I think I did a mistake in setting parameters. Could you please help me? Thanks a lot!

valeriansaliou commented 2 years ago

After uploading your file, can you append a random "?number" as a GET parameter at the end of your file URL and try checking if it is there? Just to make sure that you bypass our CDN cache for tests.

Also, can you please share an excerpt of the code that uploads the file via HTTP PUT?

ehsan-mohammadi commented 2 years ago

@valeriansaliou Thanks for the quick response. Currently, I just try to do PUT request with the Postman and if is correct, I'll try to write a code to do it. I append a random number at the end of the file URL, but it gives me Oops! This file has been deleted. It may have expired as it was too old, or was removed by our systems because it could be harmful. error.

Are my postman request and parameters correct? I send my PUT request with these 5 parameters: file: is a uploader - I click on the browse button and select my file. content-type: is a string - type of my files is the same as the type that I sent in generateURL request. name: is a string - name of my files is the same as the type that I sent in generateURL request type: is a string - type of my file that is the same as the type that I sent in generateURL request url: is a string - resource URL

valeriansaliou commented 2 years ago

What's the file you are trying to upload? We may auto delete some files due to viruses.

ehsan-mohammadi commented 2 years ago

@valeriansaliou It's just a jpeg image.

ehsan-mohammadi commented 2 years ago

@valeriansaliou Do I have to add any authorization in my parameters? I saw the PUT request has some X-Amz headers to authorize the request. But should I add my identifier:key authorization in order to have a valid request?

valeriansaliou commented 2 years ago

No, this is not neeeded. Whenever you HTTP PUT the file, the server replies w/ 200? If so, then this means the file was successfully uploaded.

I'd need to see some of your code handling the upload and (if any) file retrieval to help you more on that.

ehsan-mohammadi commented 2 years ago

@valeriansaliou

1) At first, I try to generate a signed URL. So, I use this code:

var data = {
    namespace: "upload",
    from: "plugin",
    identifier: 'my plugin identifier UID',
    id: 'some id',
    file: {
        name: input.file.name,
        type: input.file.type
      }
};

crispClient.bucket.generateBucketURL(data)
.then(function (state) {
    output(state);
}).catch(function (error) {
    output(error);
});

2) After the signed URL was generated, the bucket::url:upload:generated event is called and it gives me some information like resource and signed URL:

crispClient.on("bucket:url:upload:generated", function (bucket) {
    console.info("Upload link generated:")
    console.log(bucket);
    console.log(bucket.url);
});

3) Then I copy signed url and open the Postman application and add paste this URL and set the type of request to PUT. image

It automatically adds some params like X-Amz-Algorithm, X-Amz-Credential and etc. to verify my request.

4) Finally, I go to the body tab and add my params as a form-data (like this image): image

5) Then I press send and it gives me 200 OK response: image

But, if I copy the resource URL and paste it into my browser URL, it gives me just this error:

Oops! This file has been deleted.
It may have expired as it was too old, or was removed by our systems because it could be harmful.

And these are all things that I do.

valeriansaliou commented 2 years ago

Ah! It doesn't work like that that's why. You need to upload the actual image binary data to the signed URL. I don't know where you saw that you had to send JSON to this URL, this is not what it expects. You need to send binary data (all your image data, pbb multiple MB of data). Actual file data uploading, just as a browser would when uploading a file.

ehsan-mohammadi commented 2 years ago

@valeriansaliou Ah, thank you! Finally, I could upload it! Many many thanks for taking your time and solving my problem! As you said. I did a mistake because I uploaded my image as JSON. and I should upload it as BINARY.

Thanks again and have a good time!