0

I am trying to call a POST method of Moosend API from my MVC application. The Post call is working fine from Postman but throwing error from controller. The Code goes as below:

public async Task<ActionResult> CreateEmailList()
{
    try
    {
        using (var httpClient = new HttpClient { BaseAddress = new Uri(Baseurl) })
        {


            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

            using (var content = new StringContent("{ \"Name\": \"APIList\"}", System.Text.Encoding.Default, "application/json"))
            {
                using (var response = await httpClient.PostAsync("Mailing Lists/lists/create.json?apikey=0000", content))
                {
                    string responseData = await response.Content.ReadAsStringAsync();
                }
            }
            return View();

        }

    }
    catch (Exception ex)
    {
        return View();
    }

}

The Error I am getting is as below:

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-Robots-Tag: noindex, nofollow
  X-Server-ID: 2
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Headers: Content-Type, Accept, Cache-Control, X-Requested-With
  Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT
  Cache-Control: private
  Date: Thu, 25 Nov 2021 06:58:44 GMT
  Server: Microsoft-IIS/10.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 5054
  Content-Type: text/html; charset=utf-8
}}

Any suggestions on what am I missing here. Most of the POST example is similar to the code that I implemented.

MindSwipe
  • 7,193
  • 24
  • 47
Debaparna
  • 121
  • 4
  • Use fiddler with both request and check difference – Buğra Demirtaş Nov 25 '21 at 07:13
  • HTTP 500 means the problem is at their end, **not yours**. You should file a support ticket with Moosend. – Dai Nov 25 '21 at 07:48
  • That said, I imagine the problem is you need to escape/encode the space character in `Mailing Lists` to `Mailing%20Lists` (you can't use `+` to represent spaces in URL paths, only querystrings). – Dai Nov 25 '21 at 07:50

0 Answers0