Search code examples
jsontestingpostpostmanhttp-post

Send a post request on PostMan



I have the following method I want to test using PostMan

public returnType name( String var1, String var2,String var3, String var4);

I tried to send a post request to test I sent one like this but it does not work:

{
    "var1": "GU4777",
    "var2" : "HU 888",
    "var3" : "NU 8890",
    "var4" : "UJ 9909"
 }

I get this error:
I get this error: 10:11:16.588 [http-nio-8080-exec-3] WARN org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error


Can you guys please tell me the exact syntax I can use?

Thank you in advance;


Solution

  • Rest API

    In order to do what you want to do you first need a HTTP server that listens for requests on a certain endpoint let's call that endpoint /test.

    The server then must parse the JSON request body when it receives the request, extract the parameters and can then call the method name() using those parameters.

    Here an implementation of a server like this using JavaScript and express.js. You do not need a library for a simple server like that (but I have used one here by reason of simplicity) and you can implement the server in pretty much any language you like e.g. Java. While the syntax will differ in another language the idea will be the same.

    import express from "express";
    
    // create app
    var app = express();
    // middleware to parse json
    app.use(express.json())
    
    // listen for requests on the /test endpoint which you will hit with Postman
    app.post("/test", function (req, res) {
        // extract variables from json body
        const var1 = req.body.var1;
        const var2 = req.body.var2;
        const var3 = req.body.var3;
        const var4 = req.body.var4;
        // call method
        name(var1, var2, var3, var4);
        // send response with HTTP code 200
        res.status(200).send({
            "message": `Executed name() with var1 = ${var1}, var2 = ${var2}, var3 = ${var3}, var4 = ${var4}`  
        });
    });
    
    // the function that you want to call on the server
    function name(var1, var2, var3, var4){
        console.log(var1, var2, var3, var4);
    
    }
    
    // start HTTP server and listen for requests on port 3000
    const port = 3000;
    app.listen(port, () => {
        console.log(`Listening on port ${port}`);
    });
    

    You then need to start your server. In this case you do that by executing node server.js and then you can send requests to the server using Postman. First, put your JSON payload (= the JSON from your question) into the request body and hit the localhost:3000/test route with a POST request. You should then receive a response with status code 200.

    enter image description here

    On the server side you can observe this: enter image description here

    RPC/ gRPC

    In order to "directly" invoke a function on a server you might wanna have a look at RPC or gRPC for your preferred language.