Creating outbound email w. attachment fails (multipart/form-data)

Hello

I am trying to create an outbound email with attachments via Freshdesk API using vb.net code and Multipart Formdata content.

The request fails w. status 400 errormessage:
{"description":"Validation failed","errors":[{"field":"subject\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"}]}"

Why does Freshdesk not recognize, that the \r\n is the carriage return and linefeed from the content?
Does it need to be embraced by "???

This is my requests content:

@"------------------------------8dad7d6924bcfcc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=subject

Test
------------------------------8dad7d6924bcfcc
Content-Disposition: form-data; name=““attachments””; filename=TestFile.pdf; filename*=utf-8’'TestFile.pdf
%PDF-1.7
4 0 obj
(Identity)
endobj
5 0 obj
(Adobe)
endobj
8 0 obj

And this is my vb.net code:

  Dim client As System.Net.Http.HttpClient = New System.Net.Http.HttpClient()
  client.BaseAddress = New Uri(EndPoint)
  client.DefaultRequestHeaders.Accept.Clear()
  client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
  client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth)
  
  Dim request As System.Net.Http.MultipartFormDataContent = New System.Net.Http.MultipartFormDataContent("----------------------------" + DateTime.Now.Ticks.ToString("x"))
  request.Add(New System.Net.Http.StringContent("Test"), "subject")
  request.Add(New System.Net.Http.StreamContent(System.IO.File.OpenRead(files(0))), "attachments[]", (New FileInfo(files(0)).Name))
  
  Dim response As System.Net.Http.HttpResponseMessage = client.PostAsync("", request).Result
  status = response.StatusCode
  result = response.Content.ReadAsStringAsync.Result
  header = response.RequestMessage.Content.ReadAsStringAsync().Result

I see that adding a StreamContent does add double " and Freshdesk is not complaining about the attachments part.
How to achieve that the subject part is recognized as valid aswell?

Looking forward to your help / answers.

Best,
Thomas

I already found a solution for my issue. You need to embrace the elements with double quotation marks to make it work.
Then the requests content does have quotation marks aswell.
image

So thread could actually be closed - Nonetheless, it would be interesting, if Freshdesk or HttpClient does not stick to the rules.
Is it mandatory to embrace the field names with these quotation marks or should Freshdesk actually accept the requests even without these quotation marks?

1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.

Hey @ThomasH,

Is it mandatory to embrace the field names with these quotation marks or should Freshdesk actually accept the requests even without these quotation marks?

Yes, the solution at the moment is to embrace field names with quotations.