1

I am trying to make an HTTP Post request using any scripting language and want to Parse some content in returned response. An the content type is "application/x-www-form-urlencoded".

here is my sample code which i had written using bean shell.

 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.HttpClientBuilder;


 int responseCode = Integer.parseInt(String.valueOf(prev.getResponseCode()));

    if(responseCode=="200"){
 Status="OK";
    }else {
 Status="Fail";
    }

 HttpClient httpClient = HttpClientBuilder.create().build();
 try{

 StringEntity data =new StringEntity("client_id=sample!&client_secret=abc1&response_type=token&grant_type=creden");




 HttpPost request = new HttpPost("https://<<sample_Application URL>>");
 request.addHeader("content-type", "application/x-www-form-urlencoded");
 request.setEntity(data);
 HttpResponse response = httpClient.execute(request);
 log.info("response :" +response);
 }catch(Exception e){
 log.info("ExceptionKPI :" +e);
 }

Thanks in advance. :)

Hari
  • 123
  • 1
  • 14

1 Answers1

0

You're 99% there, now you need to get the response as a String like

String responseBody = EntityUtils.toString(response.getEntity());
// do what you need with the responseBody

If you are uncertain which scripting language to use - go for Groovy as it is more modern in terms of supporting Java 6+ language features, has nice syntax features and has much better performance.

References:


My expectation is that you'd better use normal JMeter's HTTP Request samplers for executing HTTP Post requests as given the virtual user context you will need to take care of headers, cookies, cache, etc. so your script will not be that simple.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133