1

I have this class in my .NET application to send some data from client(.NET) to server(Spring) :

    class NetworkController
    {
        private static readonly HttpClient client = new HttpClient();
        public static async Task SendUserDataAsync()
        {
            var values = new Dictionary<string, string>
            {
                { "firstName", "sunny" },
                { "lastName", "leone" },
                { "timeStamp", "test" }
            };
            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync("http://localhost:8080/user", content);
            var responseString = await response.Content.ReadAsStringAsync();

        }
    }

Reference

And in my Spring Boot application, I a class called User :

@Entity
public class User
{
    @Id
    private String firstName;
    private String lastName;
    private String timeStamp;
    public User(){}
    @Override
    public String toString() {
        return "firstName : "+this.firstName + "\n"+"lastName : " + this.lastName;
    }
}

In my rest-controller I have this method to insert User :

@PostMapping("/user")
    User addUser(@RequestBody User user)
    {
        System.out.println(user);//this always prints an empty line, maybe receiving nothing
        return userRepository.save(user);
    }

I get this warning Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

I have created this class(with the concept of Spring) in .NET, but it seems no use :

class User
    {
        String firstName;
        String lastName;
        String timeStamp;

        public User()
        {
            firstName = "1"
            lastName = "2"
            timeStamp = "test"
        }

    }

Wouldn't sending an object instead of dictionary be more gentle and tidy ? How to do so ?

How can I resolve this problem ?

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • It seems that when a dictionary is sent it is not serialized as json. This answer suggests to use Json.NET in order to serialize objects to json -- https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body – alfcope Dec 06 '19 at 14:00

1 Answers1

1

In your .NET application, the line var content = new FormUrlEncodedContent(values); indicates that the request will have a HTTP header Content-Type set to application/x-www-form-urlencoded.

It means the data stored in var values = new Dictionary... will be formatted by .NET as a query string such as firstName=sunny&lastName=leone&timeStamp=test.

That is what your Sprint server receives. However it wants to receive JSON data, not a query string. So it complains.

In order to get rid of the miscommunication, your .NET application should send JSON data, such as {"firstName": "sunny", "lastName": "leone", "timeStamp": "test"}, as expected by the Spring server.

Here is an example code:

HttpClient client = new HttpClient();

object anonymousObject = new
{
    firstName = "sunny",
    lastName = "leone",
    timeStamp = "test"
};

string jsonContent = JsonConvert.SerializeObject(anonymousObject);

var request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:8080/user");
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

HttpResponseMessage response = await client.SendAsync(request);

Console.WriteLine(await response.Content.ReadAsStringAsync());

You need to install the package Newtonsoft.Json in order to call JsonConvert.SerializeObject(anonymousObject), as pointed by this SO answer mentionned by @alfcope.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Guillaume S.
  • 1,515
  • 1
  • 8
  • 21