Results 1 to 1 of 1

Thread: Spring.Rest - how to post with url encoding instead of multi-part

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Question Spring.Rest - how to post with url encoding instead of multi-part

    I am trying to do a PostForMessageAsync with either a dictionary or newtonsoft json object (though I don't really care what I use) of values to a server, and send them URLENCODED (C# Winforms, btw). First, the default behavior is multipart, so I can't just take the easy path, and call the post with a url and a dictionary, I have to start building HttpHeaders and HttpEntities. The first problem is the docs refer to HttpEntity as a typed class, i.e. HttpEntity<T>, which it is not. Oh, but perhaps that's because the only docs on the class are for Java. But I digress . . .

    To start, I create a rest template and add several format converters, including NJsonHttpMessageConverter. In my server call function, I can do the following:
    Code:
    var headers = new HttpHeaders();
    headers.ContentType = MediaType.APPLICATION_FORM_URLENCODED;
    
    IDictionary<string, object> parts = new Dictionary<string, object>();
    parts.Add("token", loginToken);
    parts.Add("service_type", string.Format("{0}", (int)shareType));
    parts.Add("message", message);
    var entity = new HttpEntity(parts, headers);
    restTemplate.PostForMessageAsync(url, entity, r => {
                        // snip, check r.Error
                    });
    This call works, but it does not send it urlencoded as requested, it sends it in multi-part mime, which the server throws up on.

    So I also tried the following (same header)
    Code:
    var json = new JObject();
    json.Add("token", loginToken);
    json.Add("service_type", string.Format("{0}", (int)shareType));
    json.Add("message", message);
    var entity = new HttpEntity(parts, headers);
    restTemplate.PostForMessageAsync(url, entity, r => {
                        // snip, check r.Error
                    });
    This call fails, with the dreaded "no converter available" (actual error text: "Could not write request: no suitable IHttpMessageConverter found for request type [Newtonsoft.Json.Linq.JObject] and content type [application/x-www-form-urlencoded]"). What? I guess that's better than just ignoring the content type when it's a dictionary. For kicks and giggles, I tried a third round, changing the MediaType in the header to APPLICATION_JSON. It also worked, but of course, the server had no clue how to handle straight up json.

    What's the path to get my data to the server in a URLENCODED format? Spring claims to make REST easy . . .

    Thanks,
    Greg
    Last edited by bryantg; 08-01-2013 at 11:20 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •