I have been trying to insert files using Salesforce Chatter REST API but its been documented for curl. I am unable to find how to do the same in Angularjs using $http with visualforce. I am not able to set the request header and body properly. Please provide the header and body contents for the request, if this has to be done using javascript rather than curl.
Example for creating a new Document
curl https://yourInstance.salesforce.com/services/data/v23.0/sobjects/Document/
-H "Authorization: Bearer token"
-H "Content-Type: multipart/form-data;boundary=\"boundary_string\""
--data-binary #newdocument.json
Example request body for creating a new Document
This code is the contents of newdocument.json. The binary data for the PDF content has been omitted for brevity and replaced
with “Binary data goes here.” An actual request contains the full binary content.
--boundary_string
Content-Disposition: form-data; name="entity_document";
Content-Type: application/json
{
"Description" : "Marketing brochure for Q1 2011",
"Keywords" : "marketing,sales,update",
"FolderId" : "005D0000001GiU7",
"Name" : "Marketing Brochure Q1",
"Type" : "pdf"
}
--boundary_string
Content-Type: application/pdf
Content-Disposition: form-data; name="Body"; filename="2011Q1MktgBrochure.pdf"
Binary data goes here.
--boundary_string--
Related
I am trying to send an array as value for a hash key in a POST multipart request.
input = {"attribute"=> ["Countries:India", "Category:Can"]}
post("url")
.request_format_multipart_form.payload(input)
This works when using Ruby's HTTP and also POSTMAN. I can also see the differences between how Postman and Workato handles the form data.
Postman:
----------------------------428750340837882951989223
Content-Disposition: form-data; name="attribute"
Category:Test
----------------------------428750340837882951989223
Content-Disposition: form-data; name="attribute"
Countries:India
----------------------------428750340837882951989223--
Workato:
------RubyFormBoundaryhokO7A27Xb6cdSEz
Content-Disposition: form-data; name="attribute"; filename="attribute"
Content-Type: Category:Can
Countries:India
------RubyFormBoundaryhokO7A27Xb6cdSEz--
Why does Workato consider an array as a file? or am I wrong with the call?
I solved this by changing the request type to application/x-www-form-urlencoded and encode the input parameters as form data in the body (Using encode_www_form).
input = {"attribute"=> ["Countries:India", "Category:Can"]}
input = input.encode_www_form
post("url",input)
.request_format_www_form_urlencoded
I am currently trying to submit a request using the Swagger Inspector using multipart/form-data header to allow the submission of a file in conjunction with json data.
The JSON body of my request looks like:
And the headers with file upload look like:
For requests where I am just sending json to the server the Content-Type header is set to application/json and it is able to read from the body box. However I do not understand how this interface allows me to specify that the information coming from the body field is json and despite there being files on on the request.
I have seen requests that define multiple data types using the Conetent-Disspostion header, that look this this (reffenced from this Stack Overflow Post):
POST / HTTP/1.1
[[ Less interesting headers ... ]]
Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
Content-Length: 834
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text1"
text default
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text2"
aωb
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain
Content of a.txt.
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html
<!DOCTYPE html><title>Content of a.html.</title>
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file3"; filename="binary"
Content-Type: application/octet-stream
aωb
-----------------------------735323031399963166993862150--
My question would be how do I create a request in swagger inspector that accepts multiple data types? It seems like I would need to set mulple sections in the body separated by boundaries with multiple Content-Disposition and Content-Type's for each. Would there be a cleaner way to do that through the Swagger Inspector interface? Or am I going about this in the wrong way?
Thanks!
Swagger Inspector currently supports multipart/form-data requests containing one or more files. It does not support arbitrary body parts in multipart requests (e.g. a file + JSON or text data). You'll need to use another HTTP client to test such requests.
You can submit feature request for Swagger Inspector here:
https://community.smartbear.com/t5/Swagger-Inspector-Feature/idb-p/SwaggerInspectorFeatureRequests
I'm facing an issue trying to send multiples files from a ReactJS App to a Symfony Backend.
I upload two files but only one is visible in my Symfony backend.
Data are sent from a ReactJS dropzone, I well checked that the two files are sent via formData, I well used the 'content-type': 'multipart/form-data' to post the data.
In Chrome Newtwork Tab, the Form Data details shows that two files are well attached to the request :
------WebKitFormBoundaryTif9sihCtI30UXXS Content-Disposition: form-data; name="file"; filename="glacier-583419_960_720.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryTif9sihCtI30UXXS Content-Disposition: form-data; name="file"; filename="image-ile.jpg" Content-Type:
image/jpeg
Nevertheless, in my Symfony backend the File Parameter Bag only shows one file :
return $request->files;
Serialized response :
{
"parameters": {
"file": {
"test": false,
"original_name": "glacier-583419_960_720.jpg",
"mime_type": "image/jpeg",
"error": 0
}
},
"file_keys": [
"error",
"name",
"size",
"tmp_name",
"type"
]
}
As you can see, in "parameters", only one file is present instead of two.
Is anybody already encountered this problem ? I don't have any idea of what could be the issue ?
I solved my issue, it was trivial but it could help someone else for future readings.
As per indicated in the Form Data sent :
------WebKitFormBoundaryTif9sihCtI30UXXS Content-Disposition: form-data; name="file"; filename="glacier-583419_960_720.jpg" Content-Type: image/jpeg
------WebKitFormBoundaryTif9sihCtI30UXXS Content-Disposition: form-data; name="file"; filename="image-ile.jpg" Content-Type: image/jpeg
Each element added in the Form Data had the same name value.
Here is the problem :
var tempFormData = new FormData();
setFormData(acceptedFiles.map((file)=>{
tempFormData.append('file', file);
}));
Corrected code :
var tempFormData = new FormData();
setFormData(acceptedFiles.map((file)=>{
tempFormData.append(file.name, file);
}));
So the error was not coming from Symfony but from front-end App not well formatting the data sent.
I have a problem with the sending of „Post” by postman
The request is constructed in this way:
my request
When I add various headers, I receive always the response „no data”.
When I send the file by the website, on which I tried to send the request, I receive always the returnable file – sending exacly the same file which I sent by postman.
The Problem is that when I don't send the file from the website, I receive the request „no file” and even as I send the request from postman without the file, I receive the request „no data”
So how can i send the file ?
request headers
The field containing the file has a name, and it's not "Content-Disposition." Check the web site HTML, see what the name of the field is, and enter it as the name of the File field in Postman. Note that the file field name appears in the request body, not the headers. It will look something like this:
------WebKitFormBoundaryzp81ydREocuBHzYN
Content-Disposition: form-data; name="datafile"; filename="0041e2b.jpg"
Content-Type: image/jpeg
In this case the field name is "datafile."
From an AngularJS webapp I do an http post request like this:
Response Headers:
Content-Type is application/json
Request Headers:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Form data is:
json:{"id":100,"value":"https://myweb/go/1.0-1/Photo/Spa_03.jpg"}
Response is like:
{"id": 100, "imageBytes": "IMAGE BYTES"} where IMAGE BYTES are the bytes of the image whose URL is supplied in the request.
I get an HTTP success (200) but an error in the angular code (in fromJson / defaultHttpResponseTransform). Error is SyntaxError: Unexpected token.
I suppose that the response content-type is incorrect.
But could someone tell me what I must set ? And how ?
Regards.