none
How to execute a PUT from a WPF .NET 4 application and get back a JSON string? RRS feed

  • Question

  • I have a WPF application written in C# .NET 4.0 (but also has to work in .NET 3.5) that is using WebClient's UploadData (unsuccessfully) to pass data up to a local server and get a response.

    I suspect that I'm doing something wrong with how the arguments are specified because a different call that just uses an address and an empty byte array works just fine to return a JSON encoded object. Here's one of many attempts I've made in order to get the thing to work:

    try
    {
        string url = "http://127.0.0.1:4420/readlogs";
        string values = "uid=haggard%40msn.com&mailid=14193de4%2Dc772%2D4ee2%2D8aa3%2D8d1b06cbf6f2&entries=50&desc=true&notifications_only=true";
        byte[] bytes = Encoding.ASCII.GetBytes(values);
        using (var webClient = new WebClient())
        {
            byte[] ba = webClient.UploadData( url, "PUT", bytes );
            string resp = Encoding.ASCII.GetString( ba );
        }
    }
    catch(Exception ex)
    {
        lblJSONResponse.Content = ex.ToString();
    }
    

    The result is an exception of "The remote server returned an error: (500) Internal Server Error."

    What should I be doing differently?


    Richard Lewis Haggard

    Monday, July 2, 2018 1:30 AM

Answers

  • If you're calling a REST API then they tend to require JSON (or perhaps XML), not a raw byte array. Refer to their documentation to see what format they need. Most likely you'll need to convert your values to JSON (perhaps using JSON.NET) and then set the content type to JSON.

    If however they are using form URL encoding then your data looks fine as is but you need to set the content type.

    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    MSDN has a more full example of this.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 2, 2018 1:49 PM
    Moderator

All replies

  • Are you sure your server is expecting a PUT request?  Given your parameters, I'm guessing you should be using POST.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Monday, July 2, 2018 6:19 AM
  • The Web application through an exception that was unhandled in the code on the Web server side. The Web server swallowed the exception and through the 500 error. So, you have discover what the exception was about  that was thrown on the server side.

    But most likely you are using a wrong verb, like Post instead of Get something like that in your case.

    Monday, July 2, 2018 9:26 AM
  • If you're calling a REST API then they tend to require JSON (or perhaps XML), not a raw byte array. Refer to their documentation to see what format they need. Most likely you'll need to convert your values to JSON (perhaps using JSON.NET) and then set the content type to JSON.

    If however they are using form URL encoding then your data looks fine as is but you need to set the content type.

    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    MSDN has a more full example of this.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 2, 2018 1:49 PM
    Moderator
  • According to my client the request is definitely a PUT.

    Richard Lewis Haggard

    Monday, July 2, 2018 6:01 PM
  • Thanks. I tried that but no joy.

    Richard Lewis Haggard

    Monday, July 2, 2018 6:01 PM
  • The data you're passing looks more like a query string. Can you get a sample request from your provider and then compare it to what you're sending. I suspect they are expecting something different than you're sending. I don't know that we're going to be much help here because we have no way of knowing how their custom API is defined.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 2, 2018 6:13 PM
    Moderator
  • Did you try encoding the parameters as JSON?

    string values = "{ \"uid\": \"haggard@msn.com\", \"mailid\": \"14193de4-c772-4ee2-8aa3-8d1b06cbf6f2\", \"entries\": 50, \"desc\": true, \"notifications_only\": true }";


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Monday, July 2, 2018 6:16 PM
  • Correction, this was the problem. After adding the header specification there was another problem hiding in the server in that it did not recognize the user ID and gracefully handled the situation by throwing an exception. I'd incorrectly assumed that the header specification was not helping but it turned out to be essential.

    Richard Lewis Haggard

    Monday, July 2, 2018 11:41 PM