retrofit2 with 2 multiparts - JSON and File is failed - retrofit2

I'm trying to call an external ReST service.
retrofit_version = "2.9.0"
okhttp = "3.14.9"
This is the Retrofit interface as I define it:
#Multipart
#POST("orgs/{orgUuid}/patients/{patientId}/documents")
Call<DocUploadRes> uploadDocForPatient(#Header("Authorization") String authorization,
#Path("orgUuid") String orgUuid,
#Path("patientId") Integer patientId,
#Part("metadata") RequestBody metadata,
#Part MultipartBody.Part file);
My Client call is as follow:
RequestBody metadataBody = RequestBody.create(MediaType.parse("application/json"), content);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file","Safereport", RequestBody.create(MediaType.parse("application/pdf"), file.getBytes()));
Response<DocUploadRes> response = pccPatientRestApi.uploadDocForPatient(getBearerAuthHeader(pccAccessToken), pccOrgUuid, patientId, jsonPart, filePart).execute();
When I'm running this code with retrofit I'm getting Bad Request from the server with:
status":"400","title":"Bad Request.","detail":"File type is not supported"
But when I run the same service from postman it working successfully with the following http request sent:
POST /api/public/preview1/orgs/E58A8604-38F2-4098-879E-C6BCC6D01EB8/patients/372842/documents HTTP/1.1
Host: connect2.pointclickcare.com
Authorization: Bearer iy8OUOVa46oxaYRMVYlRApqDW00m:2Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="/C:/Users/user/Desktop/RosieConnect-20-API-User-Manual-Ver-07172018.pdf"
Content-Type: application/pdf
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="metadata"
{"documentCategory":1,"documentName":"Safebeing Report","effectiveDate":"2020-05-26T08:03:49.895Z"}
----WebKitFormBoundary7MA4YWxkTrZu0gW
It seems to me that retrofit doesn't send the 'application/pdf' in the Content-Type header of the file #Part... how can it be fixed?
Any idea will be very much appreciated!
-- Update ---
It appears the the file extension is mandatory.
Problem solved by adding .pdf to the file name
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file","report.pdf", RequestBody.create(MediaType.parse("application/pdf"), file.getBytes()));

Related

Is there a way to remove the default headers from the uploaded file when we post a file using a PUT request

I have a simple put request for to upload a file to an API using a post request.
api_key = "Auth token"
request_headers = {
"Authorization": "Bearer {}".format(api_key),
"Accept": "*/*",
"Content-Type": "text/plain",
}
url = "https://graph.microsoft.com/v1.0/sites/vmt8c.sharepoint.com,01f460f4-3c8a-470d-ace6-4058b2c78952,3589d1c5-e699-45a0-b8ab-1027175c367f/drive/items/root:/test-folder/aaa/test2.txt:/content"
files = {"my_file": open("params.yml", "rb")}
res = requests.put(url, files=files, headers=request_headers)
params.yml contains:
Hello! This is a test.
The uploaded data I get in /test-folder/aaa/test2.txt :
--222e00b3de068a0895a4f0f4bbd8a992
Content-Disposition: form-data; name="my_file"; filename="my_file"
Hello! This is a test
--222e00b3de068a0895a4f0f4bbd8a992--
I would like to get only "Hello! This is a test" in the uploaded data and remove the headers and everything else.
I tried a few approaches but couldn't succeed. Please help on this.

Put with python requests without Content-Disposition:

I am sending a put request to upload a file using python requests library:
f = open(fname, "rb")
files = {"file": f.read()}
response = requests.put(url, auth=auth, files=files, timeout=timeout)
However in the server side I do get my file with the following header
Content-Disposition: form-data; name="file"; filename="file"
Is there a way I can get rid of this?

Is there a way I can extract formData on nodejs backend from post request coming from vuejs frontend?

My request body looks as shown below. Am trying to get filename, uploadedBy and csv file and save to my local machine as csv.
------WebKitFormBoundarypm79CqkInUFFrKPU
Content-Disposition: form-data; name="fileName"
sample.csv
------WebKitFormBoundarypm79CqkInUFFrKPU
Content-Disposition: form-data; name="uploadedBy"
someEmail#email.com
------WebKitFormBoundarypm79CqkInUFFrKPU
Content-Disposition: form-data; name="file"; filename="sample.csv"
Content-Type: text/csv
------WebKitFormBoundarypm79CqkInUFFrKPU--

Need to modify request payload in Azure APIM set body to send modified request body to BE

My APIM is receiving request payload in POST request -
and I have to format and send to backend --
Formatted payload for BE
I have to add the 2 parameters after every newreq line and append 2 new lines after GET request line. how to achive this in apim.
newreq
Content-Type: application/http--I have to append these parameter in payload
Accept: application/json
GET Abc?$format=json HTTP/1.1
newreq
Content-Type: application/http
Accept: application/json
GET Abc?$format=json HTTP/1.1
endnewreq
You can read request body first and then append the parameters to object.
<set-body>#{
var requestBody = context.Request.Body.As<JObject>(preserveContent:
true);
requestBody ["Content-Type"] = "Your Value";
requestBody ["Accept"] = "Your Value";
return requestBody.ToString();
}</set-body>

How to parse a Http request that contain a csv file

I'm trying to handle a request that contains a csv file in the body . Exist any library in python3 that extract or parse the http request and grab the csv file. I'm a beginner in python.
My request
file = {'file': open('../file/10.csv', 'rb')}
response = requests.post('http://127.0.0.1:8080/function/preprocess', files=file)
print(response.content)
My handler
def handle(event, context):
return {
"statusCode": 200,
"body": event.body
}
Postman
---------------------------157082355573515401018537
Content-Disposition: form-data; name="file"; filename="10.csv"
Content-Type: text/csv
id,vendor_id,pickup_datetime,dropoff_datetime,passenger_count,pickup_longitude,pickup_latitude,dropoff_longitude,dropoff_latitude,store_and_fwd_flag,trip_duration
id2875421,2,2016-03-14 17:24:55,2016-03-14 17:32:30,1,-73.982154846191406,40.767936706542969,-73.964630126953125,40.765602111816406,N,455
----------------------------157082355573515401018537--

Resources