2

Hey guys so I have a node.js express server that listens for requests and sends responses, Initially, I made an app that sends a request from C# and gets a 200 response, Now I need to do the same in Java, so there's a string a that I need to send to the server. Here's my code

import java.util.HashMap;
import java.util.Scanner;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;


public class App {
    public static void main(String[] args) throws Exception {

        //System.out.println("Hello, World!");
        var values = new HashMap<String, String>() {{
            put("name", "jeff");
        }};

        //var objectMapper = new ObjectMapper();
        //    String requestBody = objectMapper
        //    .writeValueAsString(values);

        String a = "Hi this is a test";
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://192.168.1.46:4046/test"))
            .POST(HttpRequest.BodyPublishers.ofString(a))
            .build();
        HttpResponse<String> response = client.send(request,
        HttpResponse.BodyHandlers.ofString());

        System.out.println(response);
    }
}

I saw examples where the converted the request data to a JSON using the ObjectMapper, but I wish to do it without the ObjectMapper, when I did this .POST(HttpRequest.BodyPublishers.ofString(a)) in the node.js terminal I get this {}

Is there a way to fix this? I'm not a java developer but I tried making this for someone.

Just incase if you need to see my node.js code here it is

app.post("/test", (request, response) => {
  console.log("Req received");
  response.send('200');
  console.log(request.body)
})

OUTPUT:

Server listening at port 4046
Req received
{}

Thanks for reading! I tried searching this on other resources but I did not find anything relevant.

2 Answers2

2

Ok....if you are new to java then the first thing I recommend using a better IDE. I think Intellij idea is the most advanced IDE you will find around for your requirement.

Intellij Idea

Then I recommend using retrofit 2.0 for your requirement. I have answered one pretty netly how to use retrofit for making calls to api......Here is a nice example:

Using Retrofit

Ok...then java does not require libraries to be installed. It requires libraries to be imported. This can be done :

  1. Downloading jar files and importing this manually or
  2. Use a tool like a maven or Gradle to do this for you

For newcomers to java, I recommend using maven or Gradle. If you open a maven or Gradle project and add your required dependencies then maven or Gradle will fetch that jar files for you.

Ok....I have done your required things in a project....just pull the project and I think it will meet your requirements. Here is how to pull code from github in intellij idea.

1st step

2nd step

and paste the github url: Github Url

in the url. And intellij will fetch the projects for you...Hope this helps :)

1

So I did some research and found this!

package com.*********.xyz;

        import java.io.IOException;
        import java.util.Scanner;
        import java.net.URI;
        import java.net.http.HttpClient;
        import java.net.http.HttpRequest;
        import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        String body = "this is the message you are going to post!";

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://192.168.1.46:4046/authenticate"))
                .headers("Content-Type", "text/plain;charset=UTF-8")
                .POST(HttpRequest.BodyPublishers.ofString(body))
                .build();
        HttpResponse<String> response = client.send(request,
                HttpResponse.BodyHandlers.ofString());

        System.out.println(response);
        //Should print 200 after sending the response if the server is active
    }
}

This code works without any third-party libraries.