Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:56:38+00:00 2026-06-06T01:56:38+00:00

I have a jquery routine that calls an MVC action which will do a

  • 0

I have a jquery routine that calls an MVC action which will do a PUT/POST to an API url. The call from jQuery is fine and works as well as the call to the API using C#. A response is received from the API in JSON format when checked via Firebug/Fiddler.

How do i get that response to be sent back to the calling jQuery?

My C# code is:

 public string callAPIPut(string ApiUrl, string JsonString)
    {
        WebRequest request = WebRequest.Create(ApiUrl);

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(JsonString);

        request.ContentType = "application/json; charset=utf-8";
        request.Method = WebRequestMethods.Http.Put;
        request.ContentLength = JsonString.Length;

        Stream newStream = request.GetRequestStream();
        newStream.Write(data, 0, JsonString.Length);
        newStream.Close();

        return ""; // How do I return the JSON response from the API?
    }

When doing a GET i could use something like the following to get the response back to the calling jQuery:

response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
   serviceResponse = sr.ReadToEnd();
}
return serviceResponse;

I dont know how to return the response when doing a Put/Post?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:56:40+00:00Added an answer on June 6, 2026 at 1:56 am
    public ActionResult CallAPIPut(string ApiUrl, string JsonString)
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            byte[] data = Encoding.Default.GetBytes(JsonString);
            byte[] result = client.UploadData(ApiUrl, "PUT", data);
            return Content(Encoding.Default.GetString(result), "application/json");
        }
    }
    

    or make it more intelligently, by wrapping in a custom and reusable action result to avoid cluttering your controller with infrastructure plumbing:

    public class ApiResult : ActionResult
    {
        public ApiResult(string apiUrl, string jsonData)
            : this(apiUrl, jsonData, "PUT")
        {
        }
    
        public ApiResult(string apiUrl, string jsonData, string method)
        {
            ApiUrl = apiUrl;
            JsonData = jsonData;
            Method = method;
        }
    
    
        public string ApiUrl { get; private set; }
        public string JsonData { get; private set; }
        public string Method { get; set; }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            var contentType = "application/json";
            response.ContentType = contentType;
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = contentType;
                byte[] data = Encoding.Default.GetBytes(JsonData);
                byte[] result = client.UploadData(ApiUrl, Method, data);
                response.Write(Encoding.Default.GetString(result));
            }
        }
    }
    

    and now your controller action simply becomes:

    public ActionResult CallAPIPut(string apiUrl, string jsonString)
    {
        return new ApiResult(apiUrl, jsonString);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery.ajax routine that calls a php script. The php script does
I have an ASP.Net MVC 3 application that uses a jQuery .ajax call to
I have jquery with class= modal i want to call that class when page
I have this javascript routine for ajax call, which works for the most part.
Hei guys i have this JQuery Ajax call from my view and it looks
I have a form that uses a jQTools overlay and jQuery .ajax submission routine.
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have jQuery to select all font elements that are children of the element
I have jquery validation code which is working fine in ff but the same
I have multiple ajax forms on page that cointain same elemets, I have jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.