0

Code :

This method i'm using to send data using post method to my web api :

HttpUtility.UrlEncode("email+12@domain.com")
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(apiUrl, content);
response.EnsureSuccessStatusCode();

Then in web api method :

HttpContent requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result;
var query = HttpUtility.ParseQueryString(jsonContent);
string email = HttpUtility.UrlDecode(query.Get("email"));

result :

email 12@domain.com

Encode method removes + char from string, why ?

Edit : before encoding :

email+12%40domain.com

Bob Swager
  • 884
  • 9
  • 25
  • 2
    Please read [ask] and provide a [mcve]. It seems you're not using the return value of `HttpUtility.UrlEncode`, thus sending an unencoded `+` over the wire, which is decoded by the other end as a space character. – CodeCaster Apr 11 '17 at 14:05
  • Thanks. I edited my question. – Bob Swager Apr 11 '17 at 14:12
  • 2
    What is the question? The encoded string is just fine. You asked to encode the string, the string was correctly encoded. The server-side code though is weird - why are you using `Request.Content` instead of just reading the parameter? MVC/WebAPI take care of *decoding* parameters – Panagiotis Kanavos Apr 11 '17 at 14:13
  • 2
    You're still not using the return value of `HttpUtility.UrlEncode()` after the edit. Anyway it looks like a case of double decoding the data. – CodeCaster Apr 11 '17 at 14:14
  • 1
    Forgetting to send the encoded string to the server can cause problems – Panagiotis Kanavos Apr 11 '17 at 14:16
  • 1
    [A plus sign needs to get encoded as "%2B" in the URL because a plus otherwise represents a space](http://stackoverflow.com/a/124027/1997232). – Sinatr Apr 11 '17 at 14:22
  • @Sinatr yes, that's exactly what the OP is trying to do. – CodeCaster Apr 11 '17 at 14:40
  • Thanks @Sinatr :) – Bob Swager Apr 12 '17 at 09:21

0 Answers0