How to upload a Salesforce File using REST API properly? - rest

GET /services/data/v47.0/sobjects/ContentVersion/0000v00000MFIVgAAP/VersionData from this request I will get binary file content, but how can I use this content to recreate the file properly (using request to POST /services/data/v47.0/sobjects/ContentVersion)?
I uploaded .docx through the UI and fetch its content.
Then I uploded that content using the request:
Endpoint
/services/data/v47.0/sobjects/ContentVersion
Headers
Content-Type:multipart/form-data; boundary=boundary_string
Accept: application/json
Body
--boundary_string
Content-Disposition: form-data; name="entity_content";
Content-type: application/json
{"PathOnClient":"MyFile.docx"}
--boundary_string
Content-Type: application/octet-stream
Content-Disposition: form-data; name="VersionData"; filename="MyFile.docx"
Binary data from the request to GET /services/data/v47.0/sobjects/ContentVersion/0000v00000MFIVgAAP/VersionData.
--boundary_string--
The request above is executed with a success status code, and the file was created successfully, but when I downloaded this file from UI, it became damaged :(
There may be something in the request body that I missed (but I don’t know what exactly is wrong), so the file creates damaged.

Related

Difference between Content-Disposition: Form-data and attachment

I am building out an apache HttpClient for a REST service I have, and a app that will be using my service is sending a POST request using "content-disposition: attachment; name="file"; filename="file.txt".
Currently my Client is using "Content-Disposition: form-data; name="file"; filename="test.txt". Currently my request is multipart/mixed, where I am sending a file, and JSON.
Ideally I would need to have my client using the attachment one, but after much research, I have not found anything meaningful. What exactly is the difference between the two content-dispositions?
Attachment is for responses, form-data is for requests. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Postman file upload boundary

I'm trying to upload a file with my REST POST request. It is not working out. When i try to look at the request postman give:
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 44262
Content-Type: multipart/form-data; boundary=--------------------------085877645320272030820682
Host: localhost
Why there is a boundry? why postman doesn't return the file that i uploaded?
Adding an attachment as 'binary' would solve the POST request problem in your case.
If you would like to download the same attachment, there should be an GET request towards the API.
If that doesn't solve, you should describe your issue better with some pictures to understand better.

Insomnia REST Client - Set "Content-Type" for multipart/form-data

TL;DR
How can I set the Content-Type headers for each individual file/input/text in a multipart/form-data request (in Insomnia)?
I'm trying to POST to the OneNote API (HTTP description) using the Insomnia Rest Client. Per the documentation, I need to submit a multipart/form-data request with one file/text with headers:
Content-Type: text/html
Content-Disposition: form-data; name=presentation
and another with the headers:
Content-Type: application/inkml+xml
Content-Disposition: form-data; name=presentation-onenote-inkml
Here is a screenshot of what I am trying:
As you can see, the API returns an error with No Content-Type leading me to believe that the Content-Type header is not set. The debug information is below:
The POST data is hidden, which does not allow me to see the Content-Type.
Solution: When you use POST with the file arguments, this works. I don't know why.

How to send request with request payload instead of form data? (iphone)

Usually when I look at the POST requests that are send to a webpage by my browser, they often include Form Data. I then make my own NSURLRequest appending httpbody with similar paramaters, and thus send POST requests to the page I need. However I recently encountered with a situation where a POST request doesn't havve a Form Data at all, but Request Payload instead. I have no idea what it means. Should I send the request appending httpbody with the paramaters from request payload? So far it hasn't been working for me though.
Here how this request payload looks:
------WebKitFormBoundarySomeLettersHere
Content-Disposition: form-data; name="recipient"
theRecipient
------WebKitFormBoundarySomeLettersHere
Content-Disposition: form-data; name="recipients"
theRecipients
------WebKitFormBoundarySomeLettersHere
Content-Disposition: form-data; name="inReplyToPimId"
thePimid
------WebKitFormBoundarySomeLettersHere
Content-Disposition: form-data; name="message"
theMessage
------WebKitFormBoundarySomeLettersHere--
Among the request headers I found interesting:
Accept-Encoding:gzip,deflate,sdch
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarySomeLettersHere
Cookie:JSESSIONID=someId; PPLoggedIn=myId; pimConversation=true
I checked among the cookies I have, that "pimConversation" is missing. Maybe it is the problem? But again I made such cookie and add it to cookies storage.
What do you think I am doing wrong? Thank you in advance!

Fiddler multipart/form-data with auth token asp.net web api 2

I have been struggling with this for few days now, (new to fiddler)
My Url looks like this:
mywebservice/miclaim/casedetail/GetCaseDetail/638110079?apikey=MiClaimUK&token=ZD31MsFiLrFA2hCZShBJ7i4iinqeRxfYNrIsDHWriQM=
Now this is a multipart/form-data content type, and i tried a few things to submit my form data like this: (I have no problem submitting file though.. its just the form data along with file!)
adding the values after the token stuff in the query
LossItemId=1&Description=d&ClaimedAmount=1234.5&WherePurchased=reading&BasisOfValuation=basis&Status=sta
or just adding them on the request header but nothing seems to work, I still don't get my form data values in the controller...
I think it must be fairly obvious and usual thing in fiddler to do, but why am in having so much trouble? What am I missing?
Note: I can test my app by a test client using html form ..enctype="multipart/form-data" method="POST"> and it works... but not in Fiddler??
Had to revisit this problem in my project in the context of some other issue..and finally got it working this time around
(for those who might come across a similar issue):
---------------------------acebdf13572468
Content-Disposition: form-data; name="ToDo"
Content-Type: application/json
{"ToDoId":32,"InstructionId":6300460,"Description":"Description","Comment":"Comment","DueDatetime":"2014-02-28T16:44:52.8140079Z","SubmittedDatetime":"2014-02-28T16:44:52.8140079Z","StatusCode":10,"Media":[{"MediaId":0,"MediaDescription":"abc","CreatedDate":"2014-02-28T16:44:52.815008Z","MediaType":"Doc","UrlPath":null},{"MediaId":0,"MediaDescription":"foo","CreatedDate":"2014-02-28T16:44:52.815008Z","MediaType":"Image","UrlPath":null}]}
---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="abc.txt"
Content-Type: text/plain
<#INCLUDE *C:\uploads\abc.txt*#>
---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="Foo.txt"
Content-Type: text/plain
<#INCLUDE *C:\uploads\Foo.txt*#>
---------------------------acebdf13572468--