2

I have an issue with postman and c# code. I have two post calls to an API that must in the end make a callback to another API (webhook).

I tried to launch the two calls through Postman and i do obtain correctly the callback response. My issue is that when I use this code I do not have any callback response but I obtain 200 message from the server I call. I have the same issue with all implementations of the http post calls I sent and get the same issue.

    public static void Main(string[] args)
    {
        // first call 
        var client = new RestClient("http://xxxxxxx/emails/attachments/");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Authorization",
            "Bearer theToken");
        request.AddFile("attachment",
            "C:/Users/..../pdf.pdf");
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
        var attachmentId = response.Content;

        // second call
        client = new RestClient("http://xxxxxxx/emails/");
        client.Timeout = -1;
        request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Authorization",
            "Bearer theToken");
        request.AddParameter("application/json",
            "{\r\n  \"subject\": \"email with attachment\",\r\n  \"body\": \"Hi !!!l\\r\\n\",\r\n  \"received_on\": \"2021-05-24T14:07:01.5874416+02:00\",\r\n  \"author\": {\r\n    \"name\": \"[email protected]\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"sender\": {\r\n    \"name\": \"[email protected]\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"to\": [\r\n    {\r\n      \"name\": \"[email protected]\",\r\n      \"smtp_address\": \"\"\r\n    }\r\n  ],\r\n  \"cc\": [\r\n    {\r\n      \"name\": \"\",\r\n      \"smtp_address\": \"\"\r\n    }\r\n  ],\r\n  \"attachments\": [\r\n       " +
            attachmentId + "\r\n  ]\r\n}\r\n", ParameterType.RequestBody);
        response = client.Execute(request);
        Console.WriteLine(response.Content);

    }
}

Any idea about what's going wrong ? the weird thing is that I have a 200 response for each calls i make.

8
  • Well, I think it is better to open Fiddler, catch requests and compare it. Also, try to not set Timeout for request - it will be default value and check request Commented May 27, 2021 at 14:55
  • request.AddHeader("Authorization","Bearer theToken); Seems like there is a problem with your quotation marks here, although that's probably not the issue. Commented May 27, 2021 at 14:57
  • Also, do you get a response from the first call, or nothing at all both times? Commented May 27, 2021 at 15:00
  • An attachment starts with a new line containing two dashes. See : learn.microsoft.com/en-us/previous-versions/office/developer/… Commented May 27, 2021 at 15:04
  • i have response for both calls with 200 status code ... it's very weird. and if i have a 200 response, I should receive a callback to a webhook url ... but i don't get it Commented May 27, 2021 at 20:35

1 Answer 1

1

I solved my issue with this code:

using (var client = new HttpClient())
{
    using (var form = new MultipartFormDataContent())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
        
       var file = File.OpenRead(attachmentPath);
       byte[] fileBytes = new byte[file.Length];
       form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "attachment", Path.GetFileName(attachmentPath));
       HttpResponseMessage response = await client.PostAsync(url, form);
       response.EnsureSuccessStatusCode();
                             
       var output = await response.Content.ReadAsStringAsync();
       PostEmailAttachmentResponse returnValue = new PostEmailAttachmentResponse();
       returnValue.Id = Int32.Parse(output);
       return returnValue;
  }
}

My previous code wasn't returning error message when i sent attachment. there was issue on the server side which was not returning error.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't put HttpClient in a using block. Although it is disposable, it's actually reentrant, and one static instance can and should be (re)used for your service. I.e. don't dispose it
Yeah, don't know about your original issue. You even state this was an issue on the server side. These kind of questions are effectively impossible to fully answer, as they are application specific and require debugging on your environment. (TBH that's why the actually have no real value to Stack Overflow: question not answer with be really useful to others)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.