Knowledge Base Article

> Knowledge Base > Rapise

Created By  inflectra.adam Thursday, March 2, 2017

When performing web service testing using Rapise you may need to change the HTTP headers being sent with the request. This article provides suggestions on the different ways of doing this.

When you are writing REST web service tests, you can use the following code to set the HTTP headers:

      var headers = SeS('RestHeaderTest1_Get_Session').GetRequestHeaders();
      SeS('RestHeaderTest1_Get_Session').SetRequestHeaders([{'Name': 'Content-Type', 'Value': 'application/json'}]);
      headers = SeS('RestHeaderTest1_Get_Session').GetRequestHeaders();
      Tester.Message(headers[0].Value);

If you would like to change thee headers (e.g. in this case to switch the Content-Type from JSON to XML, you might try the following:

      //Set headers to XML - does not work
      SeS('RestHeaderTest1_Get_Session').SetRequestHeaders([{'Name': 'Content-Type', 'Value': 'application/xml'}]);
      headers = SeS('RestHeaderTest1_Get_Session').GetRequestHeaders();
      Tester.Message(headers[0].Value);

However that does not work, you instead should use the following:

      //Set headers to XML - does work
      headers[0].Value = 'application/xml';
      headers = SeS('RestHeaderTest1_Get_Session').GetRequestHeaders();
      Tester.Message(headers[0].Value);   

Another option that we recommend is using the new global Session object that was introduced in Rapise 5.0:

//Set original header
Session.SetRequestHeaders(...)
SeS('RestHeaderTest1_Get_Session').DoSomething(...)

//Set new headers
Session.SetRequestHeaders(...)
SeS('RestHeaderTest1_Get_Session').DoSomethingWithNewHeaders(...)

 

Article Info
  • Last Updated: 3/2/2017
  • Article ID: KB243
  • Views: 471