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\": \"dvera@mymail.fr\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"sender\": {\r\n    \"name\": \"dvera@mymail.fr\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"to\": [\r\n    {\r\n      \"name\": \"dvera@mymail.fr\",\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.

davidvera
  • 1,292
  • 2
  • 24
  • 55
  • 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 – DarkSideMoon May 27 '21 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. – thesystem May 27 '21 at 14:57
  • Also, do you get a response from the first call, or nothing at all both times? – thesystem May 27 '21 at 15:00
  • An attachment starts with a new line containing two dashes. See : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true – jdweng May 27 '21 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 – davidvera May 27 '21 at 20:35
  • Try `HttpClient`: modern, `async` and simple. Look for some of billions examples like "how to post json". – aepot May 27 '21 at 21:24
  • i also did it and i have the same issue ... my 2 calls are returning me the expected result but i don't get the call back call from the API ... – davidvera May 27 '21 at 21:36
  • thanks for your help, i finally found inspiration here https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data – davidvera Jun 01 '21 at 07:11

1 Answers1

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.

davidvera
  • 1,292
  • 2
  • 24
  • 55
  • 1
    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 – JHBonarius Jun 01 '21 at 20:53
  • 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) – JHBonarius Jun 02 '21 at 07:47