Answer the question
In order to leave comments, you need to log in
Keep Alive and Session ID in Postman - is it real?
Good afternoon fellow professionals.
Please help with experience, can anyone come across?
My task is to simply check the set of API commands (because there is no adequate and up-to-date documentation, but you need to know exactly what f-back from the server).
To start working with the server, you need to send a GET command to this address http://IP-address/rest/n , in response it will return a Session ID, which is valid for 30 seconds.
Those. is the creation of the session.
After that, I need to send one of the API commands, but to this address http://IP-address/rest/r/{Session ID obtained from the first command}
How to do this in Postman and is it realistic? I figured out how to send commands in a batch (one after another), but how, from the feedback from the previous command, to transfer data to the URL header of the next one, and is it possible to do this in Postman - I don’t understand !? Through some global variables, something also does not work.
Is there other software that can do this?
Thanks in advance for your answer,
Alexey (I'm not a programmer)
Answer the question
In order to leave comments, you need to log in
Looks like I'll answer my own question.
But it's a pity that the software is paid: https://www.soapui.org/functional-testing/properti...
Can someone else recommend something similar, free?
12 Oct. 2016
Well, apparently I will answer this question myself too.
I did not find anything free and convenient for solving my problem, so I had to write my own utility. Below is the code - suddenly, someone will come in handy in the future!
The utility reads a JSON structure from a text file (which can be edited on the fly and saved) and sends it to the server by pressing the Enter key.
Regarding "shit codes", "torn off hands", "bicycles", etc. etc. - as I wrote, I'm not a super programmer and I don't pretend to be! The utility allows me to solve my question and on this hallelujah.
If someday, some Java guru visits this page and says that everything needs to be rewritten (and most importantly, he will do it) - I will only be grateful to him, like other users of this resource.
import java.net.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;
import java.io.Console;
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.commons.codec.binary.Base64;
// Latest library can be found herecommons.apache.org/proper/commons-codec It is needed to generate the authentication string.
public class rt2{
// fname - file from which the JSON structure will be taken, sid - session ID received from the server
// json_str - read JSON structure, s - string for reading input from the keyboard
static public String fname="json.txt" , sid = "", json_str="", s;
//Method of reading file with JSON structure
public static void m_readfile (String filename) throws IOException{
int i = 0;
char a;
boolean endof = false;
System.out.println("Open file:" + filename + "\n" +"Current SID:" + rt2.sid +"\n");
StringBuilder s = new StringBuilder();
FileReaderFr = new FileReader(filename);
do {
i = Fr.read();
if (i==(-1)) break;
s.append((char) i);
} while (i!=-1);
rt2.json_str = s.toString();
fr.close();
i = s.length();
System.out.println("Message length:"+ i +"\n");
}
// r_type: POST or GET; r_URL - full URL " 192.168.1.1/rest/n ", r_login - login, r_pwd - password
// ses_id - session ID, if equal to "", then a new session must be created, if not equal, then Keep Alive request or API request
// json_structure - sent JSON structure
public static void send_request(String r_type, String r_url, String r_login, String r_pwd, String ses_id, String json_structure) {
String req = "";
//Forming a string for basic authentication
String authString = r_login + ":" + r_pwd;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
try{
// Forming an HTTP request, opening a session and sending a message
URL url= new URL(r_url);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setRequestMethod(r_type);
uc.setRequestProperty("authorization", "Basic" + authStringEnc);
uc.setUseCaches(false);
uc.setRequestProperty("content-type","application/json");
if (json_structure !="") {
uc.setRequestProperty("content-lenght", ""+json_structure.length());
}else {
uc.setRequestProperty("content-lenght", ""+req.length());
}
uc.connect();
DataOutputStream dos= new DataOutputStream(uc.getOutputStream());
dos.writeBytes(json_structure);
dos flush();
dos.close();
System.out.println("\n"+"--==JSON BODY SENT ==--"+"\n\n"
BufferedReader br = new BufferedReader (new InputStreamReader(uc.getInputStream()));
String res = null;
if (ses_id == "") {
// If the session ID is empty, then this means that the first request to create a session has arrived.
// Otherwise, it already exists and it's either a Keep-Alive request or an API request.
// Reading the response from the server and creating a string, for subsequent parsing - you need to extract the Session ID from the response.
while ((res=br.readLine()) != null)
{rt2.sid = rt2.sid + res;}
br.close();
uc.disconnect();
// Parsing the response from the server. Extraction of the session ID. In my case, the response from the server is the same and I know for sure that this is the 3rd word after the " .
String[] sid_to_use = rt2.sid.split("\"");
rt2.sid = sid_to_use[3];
} else {
System.out.println("\n"+"--==SERVER RESPONSE START== --"+"\n");
while ((res=br.readLine()) != null)
{System.err.println(res);}
System.out.println("\n"+"-- ==SERVER RESPONSE END==--"+"\n");
br.close();
uc.disconnect();
}
} catch (MalformedURLException me) {
System.err.println(me);
} catch (UnknownHostException he){
System.err.println(he);
} catch (UnknownServiceException se){
System.err.println(se);
} catch (IOException ioe) {
System.err.println(ioe);
}}
public static void main (String[] args){
rt2 rt = new rt2 ();
rt2.send_request("POST"," 192.168.1.1/rest/n ","admin","admin",rt2.sid,""); // First request to the server and getting the session ID
Timer timer = new Timer(true); // Create a daemon process that will send a Keep-Alive every 28 seconds
TimerTask task = new TimerTask () {
String res2 = null;
public void run() {
LocalDateTime time = LocalDateTime.now();
rt2.send_request("GET"," 192.168.1.1/rest/poll "+rt2.sid,"admin","admin",rt2.sid,"");
System.out.println ("-=Keep Alive sent=-" + " Time:"+ time.format(DateTimeFormatter.ofPattern("HH:mm:ss"))+" SID: "+rt2.sid+"\n ");
System.out.println( "Press ENTER to send JSON request ('q'+Enter to exit): ");
};};
timer.schedule(task,0,28000);
do {
s = System.console().readLine();
if (s.equals("q")) break;
try{
m_readfile(fname);
rt2.send_request("POST"," 192.168.1.1/rest/req "+rt2.sid,"admin","admin",rt2.sid,rt2.json_str);
} catch (IOException e) {
System.err.println(e);
} }
while (true);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question