James Wilson
James Wilson

Reputation: 1

Trying to send an attachment with my post request to the fresh desk outbound_email end point

I am trying to use RestSharp to call the outbound_email endpoint from the fresh desk API.

I want it to send an email which includes an attachment.

If I don’t include an attachment I am able to post to the end point successfully, and I do receive the email, however when I do include an attachment it fails stating bad request.

I am able to get it working correctly in Postman, so I’m wondering if the file I am adding is in the wrong format or something, but couldn’t find anything when going through the API docs.

This is my code below:

var client = new RestClient(outboundEmail);
var request = new RestRequest("", Method.Post);

request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(apiKey + ":X")));

request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "multipart/form-data");

request.AddFile("attachments[]", "filePath");

request.AddParameter("email", myEmail);
request.AddParameter("subject", subject);
request.AddParameter("description", description);
request.AddParameter("status", status);
request.AddParameter("priority", priority);
request.AddParameter("email_config_id", emailConfigID);

var response = client.Execute(request);

The error it gives me is:

"description":"Validation failed","errors": [{"field":"email\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"}, {"field":"subject\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"description\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"status\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"priority\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"email_config_id\r\n","message":"Unexpected/invalid field in request","code":"invalid_field"}]}"

I have also tried using httpClient instead of rest sharp but still get the same error.

Upvotes: 0

Views: 278

Answers (1)

James Wilson
James Wilson

Reputation: 1

After checking my request with requestbin, I could see that the difference between the postman request and the restsharp request was that in post man the name of the parameters were in quotation marks "email", where as the restsharp request didn't have them in quotations.

To resolve this I forced the quotation marks in :

    request.AddParameter("\"email\"", myEmail);
    request.AddParameter("\"subject\"", subject);
    request.AddParameter("\"description\"", description);
    request.AddParameter("\"status\"", status);
    request.AddParameter("\"priority\"", priority);
    request.AddParameter("\"email_config_id\"", emailConfigID);

Upvotes: 0

Related Questions