Gmail API: send text and HTML in one mail - gmail

Mail sending either text or HTML works perfectly fine, but when I send them both in single mail, HTML comes as an attachment named "noname.html", which contains HTML.
I have read other related questions on this topic, but couldn't find what is possibly wrong.
MIME-Version: 1.0
From: sender#gmail.com
To: receiver#gmail.com
Subject: test
Content-type: multipart/mixed; boundary="012boundary"
--012boundary
Content-type: text/plain; charset="UTF-8"
Hello plain text!
--012boundary
Content-type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<b>Hello html</b>
--012boundary--`

You want to send both HTML body and text body using gmail API. If my understanding is correct, how about this modification?
From :
Content-type: multipart/mixed; boundary="012boundary"
To :
Content-type: multipart/alternative; boundary="012boundary"
Note :
Using multipart/alternative, both parts of Content-type: text/plain; charset="UTF-8" and Content-type: text/html; charset="UTF-8" can be sent.
Reference :
https://en.wikipedia.org/wiki/MIME#Content-Type
In my environment, I could confirm that your request body which was modified to multipart/alternative worked. If this didn't work on your environment, I'm sorry.
Edit :
In order to send a text body, a HTML body and an attachment file of HTML as one email, the structure of request body can be created as follows.
multipart/mixed
multipart/alternative
text/plain
text/html
text/html (Attachment file)
Sample request body :
MIME-Version: 1.0
From: sender#gmail.com
To: receiver#gmail.com
Subject: test
Content-Type: multipart/mixed; boundary=012boundary01
--012boundary01
Content-Type: multipart/alternative; boundary=012boundary02
--012boundary02
Content-type: text/plain; charset=UTF-8
Hello plain text!
--012boundary02
Content-type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<b>Hello html</b>
--012boundary02--
--012boundary01
Content-type: text/html; charset=UTF-8
Content-Disposition: attachment; filename="sample.html"
Content-Transfer-Encoding: quoted-printable
<b>HTML sample attachment file</b>
--012boundary01--
References :
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
http://qcode.co.uk/post/70

Related

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 automate downloading attachment from Rest API GET response in SoapUI

I am using SoapUI 5.5.0 and I am trying to automate the download of an .xls attachment from a Rest API GET response.
It does not appear in the attachment tab of the response.
I tried adding "Enable MTOM | true" but the request stop working with
it.
I tried some groovy scripts but I didn't get anything out of what I tried.
**RAW RESPONSE**
HTTP/1.1 201
Set-Cookie: Design_Authorization=VeryLongToken; Max-Age=93600; Expires=Tue, 12-Jan-2021 22:33:22 GMT; Path=/Redacted; HttpOnly
Set-Cookie: JSESSIONID=bunchofnumbers; Path=/Redacted; HttpOnly
Content-Disposition: attachment; filename=SoapUI_Export_DD_20210111_153209.xls
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/vnd.ms-excel
Transfer-Encoding: chunked
After this, the response has a bunch of unreadable characters.
If I look at the XML tab I get this:
**XML RESPONSE**
<data contentType="application/vnd.ms-excel" contentLength="647680">0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7/CQAGAAAAAAAAAAAAAAAKAAAAAAAAAAAAAAAAEAAA/v///wAAAAD+////AAAAAAEAAACAAAAAAAEAAIABAAAAAgAAgAIAAAADAACAAwAAAAQAAIAEAAD///...it's very long
Adding this here since I could not get a readable format in my thank you comment below.
I had a null error on the response.getProperty('Content-Disposition').split('=')[1] line.
Since I generate and store the name of the export earlier in the testcase, I get the property and then use it.
This is what I ended with:
import org.apache.commons.io.FileUtils
def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()
// define filepath/name
exportname = testRunner.testCase.getPropertyValue("exportName")
reportfolder = (System.getProperty("user.home") + File.separatorChar + "Documents" + File.separatorChar);
def filename = reportfolder + exportname +'.xls'
def file = new File(filename)
FileUtils.writeByteArrayToFile(file, data) `
The body of the response is the file. You have to extract it, something like this:
import org.apache.commons.io.FileUtils
def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()
// define some filename
def filename = response.getProperty('Content-Disposition').split('=')[1]
def file = new File(filename)
FileUtils.writeByteArrayToFile(file, data)

retrofit2 with 2 multiparts - JSON and File is failed

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()));

How do I implement redirect image using Express?

I want to make GET request to my nodejs server.
I want to implement redirecting image.
On client I have
var body = document.getElementsByTagName('body')[0];
var childElement = document.createElement('img');
childElement.width = 1;
childElement.height = 1;
childElement.style.daiplay = 'none';
childElement.src = 'http://myendpoint.com/api/endpoint?token=token';
body.appendChild(childElement);
My endpoint should redirect client to some URL.
in server code I have
app.route('/api/endpoint').get(function(req, res) {
res.redirect('http://google.com')
});
As a result I get
General
Request URL:http://myurl.com
Request Method:GET
Status Code:302 Found
Remote Address:127.0.0.1:1340
Response Headers
HTTP/1.1 302 Found
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
Location: http://google.com
Vary: Accept, Accept-Encoding
Content-Type: text/plain; charset=utf-8
Content-Length: 41
Date: Tue, 02 Feb 2016 10:50:13 GMT
Connection: keep-alive
But client is not actually redirected..
After this request GET to redirect page happens, but I receive html in response, not being redirected..
What am I doing wrong?

Resources