How to set up a JSON upload with POST?

Asked

Viewed 1,715 times

0

I need to make an API that captures some data from the machine (OS name, cpu usage, network usage etc.) and send as JSON. I was able to capture the data and have it displayed on the console in the form of JSON, however, I never messed with the data SEND. I did some research on Httprequest, but I didn’t understand how to use it. Remembering, I’ve only been dealing with Java for about 3 months, so if you can help me understand how I can do this sending, I really appreciate it!

Guys, that’s the code I’m in right now. I’m returning a STRING with the CPUINFO class Json, but if some application "pulls" it, it will pull in String format, and not Json, right? I have how to make it return in Json?

public class ServerHTTP {

    static CPUINFO infocpu = new CPUINFO();

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(9080), 0);
        System.out.println("Executando");
        server.createContext("/teste", new MyHandler());
        server.createContext("/", new HomeHandler());
        server.createContext("/dados", new dataHandler());
        server.setExecutor(null);
        server.start();

    }

    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {

            Headers h = t.getResponseHeaders();
            h.add("Content-Type", "application/json");

            String response = "<h1> Hello World! </h1>";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }

    static class HomeHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {

            Headers h = t.getResponseHeaders();
            h.add("Access-Control-Allow-Origin", "*");
            h.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

            String response = "<h> Homepage </h>";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }

    static class dataHandler implements HttpHandler {
        public void handle(HttpExchange he) throws IOException {
            Headers h = he.getResponseHeaders();
            h.add("Access-Control-Allow-Origin", "*");
            h.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            String response = infocpu + "";
            he.sendResponseHeaders(200, response.length());
            OutputStream os = he.getResponseBody();
            os.write(response.toString().getBytes());
            os.close();
        }
    }
  • Hello. Post what you have achieved so far, with part of the code and where exactly is the doubt that the community will be happy to help :)

  • Exactly as the colleague said, post the data you have already obtained and where you need help.

  • So, it’s because what I understood so far was very theoretical, I don’t really know how to use Httprequest, and well, to confess I’m kind of lost in this request and the like. I only know I have to send the data in Json, but tbm I’m kind of without a north. What I have is more or less that (posted in the question)

  • This request you want to send is a POST? If yes, you know what data you expect to receive (Json, XML, Form Data, etc)?

  • To understand how an HTTP request works, see this link

  • Hi! It’s a JSON, but I have no idea how I’m going to send it :S

  • And, by reading, I will not send a request, I will receive a request and send the JSON as a response by POST. It is more or less that

Show 2 more comments

1 answer

0

Here’s an example of how to do a Java post using Json as the request body:

String payload = "data={" +
            "\"username\": \"admin\", " +
            "\"first_name\": \"System\", " +
            "\"last_name\": \"Administrator\"" +
            "}";
StringEntity entity = new StringEntity(payload, ContentType.APPLICATION_FORM_URLENCODED);

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("http://localhost:8080/endpoint");
request.setEntity(entity);

HttpResponse response = httpClient.execute(request);
System.out.println(response.getStatusLine().getStatusCode());

Dependencia Maven:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
  • I’m very indecisive about being good or not posting to a 3-month Java beginner using a framework, rather than a "pure" Java implementation.

  • So friend he asked for a solution, I gave one that seems simple to understand.

  • Hi! I will update the post with the code that (with the help of the Internet) I was able to do more or less. If you could help me in it, it would also be of great gratitude!

  • Friend actually json is how the data will traffic, how the person will pick it depends on the client, can pick using an object or using string there depends on the client, the important thing in this case is how Voce is sending that in the case is json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.