locked
Formatting HTTP Response Messages RRS feed

  • Question

  • User-1163741450 posted

    Hello All,

    I am creating a web api to feed data into an instant messaging system provided by a Third Party. So far i have been able to get the commands working from within the messenger but i am having some formatting issues.

    My web API is returning an HTTPreponsemessage in text/plain. It essentially dump the results as one long ArrayList. I tried doing a replace to insert line breaks but the messenger application reads them literally and i just get /r/n bits throughout the repsonse.

    Here is my WebAPI Controller Code

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.Description;
    using DiscountProject.Models;
    using System.Net.Http.Headers;
    using System.Net.Http.Formatting;
    using System.Data.Entity.SqlServer;
    
    
    
    namespace DiscountProject.Controllers
    {
    
        
    
        public class DiscountsController : ApiController
        {
            private DiscountsEntities db = new DiscountsEntities();
    
            [HttpPost]
            public HttpResponseMessage Authenticate(FormDataCollection form)
            {
                var message = form.Get("Message");
    
                if (message == "/discounts" )
                {
    
                   var ReturnedDiscounts = from d in db.Discounts
                                            where d.DiscountStartDate < DateTime.Now && d.DiscountStopDate >= DateTime.Now
                                            orderby d.DiscountPriority ascending
                                            select string.Concat(d.DiscountPriority, " - ", d.DiscountName);
    
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK,ReturnedDiscounts);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
    
    
                    return response;
                }

    Result ouput from Messenger

    ["8 - VAPE LIQUID - 2 for $2.00","11 - WILDBERRY INCENSE 1","15 - PREMIUM CIGAR DISCOUNT - BUNDLE"]

    Ideally I would prefer an output simliar to with each entry having its own line.

    "8 - VAPE LIQUID - 2 for $2.00"

    "11 - WILDBERRY INCENSE 1"

    "15 - PREMIUM CIGAR DISCOUNT - BUNDLE"

    Any advice would be appericated

    Thursday, August 17, 2017 3:16 PM

Answers

  • User-1163741450 posted

    I was able to get this figured out by adding the following.

      var ReturnedDiscounts = from d in db.Discounts
                                            where d.DiscountStartDate < DateTime.Now && d.DiscountStopDate >= DateTime.Now
                                            orderby d.DiscountPriority ascending
                                            select string.Concat(d.DiscountPriority, " - ", d.DiscountName);
    
       ---------->   var FormattedDiscounts = string.Join("<br/>", ReturnedDiscounts);
    
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK,FormattedDiscounts);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    
    
                    return response;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 17, 2017 5:25 PM

All replies

  • User-1163741450 posted

    I was able to get this figured out by adding the following.

      var ReturnedDiscounts = from d in db.Discounts
                                            where d.DiscountStartDate < DateTime.Now && d.DiscountStopDate >= DateTime.Now
                                            orderby d.DiscountPriority ascending
                                            select string.Concat(d.DiscountPriority, " - ", d.DiscountName);
    
       ---------->   var FormattedDiscounts = string.Join("<br/>", ReturnedDiscounts);
    
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK,FormattedDiscounts);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    
    
                    return response;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 17, 2017 5:25 PM
  • User1168443798 posted

    Hi Bobby,

    Thanks for sharing the solution, and I would suggest you mark your reply as answer which is the way to close a thread here.

    Best Regards,

    Edward

    Friday, August 18, 2017 7:12 AM