Forum Discussion

kitaitoa's avatar
kitaitoa
Contributor
2 years ago

How to use aqHttpRequest with utf8 encoding

I use a webservice call to manage my SQL queries during my automated tests with TestComplete.
The Dev has created the right webservice for this.
I get the response from my service in a JSON. For that I pass it in the Content-Type header: aqHttpRequest.SetHeader("Content-Type", "application/json").
I would like to pass it a utf-8 encoding as well, because I need it if my response contains accents.

I have tried several methods:

aqHttpRequest.SetHeader("Content-Type", "application/json; charset=utf-8")

But this returns a "text/html; charset utf-8. I lose the json !
or 
aqHttpRequest.SetHeader("Content-Type", "application/json")
aqHttpRequest.SetHeader("Accetp-Charset", "utf-8")
It doesn't solve the encoding.

 

This is the full code : 

function httpPostRequest(paramAdress,paramRequestBody)
{
  
  // Lecture de l'environnement
  var beginParamAdress = aqString.SubString(ProjectSuite.Variables.URL,0,aqString.Find(ProjectSuite.Variables.URL,"home.html"))
  
  // Récupération du token 
  var accesToken = getAccesToken(beginParamAdress);
  
  // Construction de l'adresse complète (environnement + WebService)
  var address = beginParamAdress + paramAdress ;
  
  // Define the request body JSON string
  var requestBody = paramRequestBody ;
  
  // Create the aqHttpRequest object
  var aqHttpRequest = aqHttp.CreatePostRequest(address);

  // Specify the Content-Type header value
  aqHttpRequest.SetHeader("Content-Type", "application/json;charset=utf-8" );
  aqHttpRequest.SetHeader("com.qualiac.auth.accessToken", accesToken);
  

  // Send the request, create the aqHttpResponse object
  var aqHttpResponse = aqHttpRequest.Send(requestBody)
  

  // Check the response:
  Log.Message("aqHttpResponse.GetHeader " + aqHttpResponse.GetHeader("Content-Type"));
  Log.Message(aqHttpResponse.StatusCode); // A status code
  Log.Message(aqHttpResponse.Text); // A body


Any ideas?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Could you also try with,

     

    aqHttpRequest.SetHeader("Accept", "application/json;charset=UTF-8");
    aqHttpRequest.SetHeader("Accept-Charset", "UTF-8");

     

     

    You say "I lose the json", how?

     

    Have you tried with another tool such as Postman, is the format correct?

  • Thanks rraghvani 
    I try this solution:

     

    aqHttpRequest.SetHeader("Accept", "application/json;charset=utf-8");
    aqHttpRequest.SetHeader("Accept-Charset", "utf-8");
    aqHttpRequest.SetHeader("Content-Type", "application/json;charset=utf-8" ); 

     

    But, it doesn't handle the encoding and the getHeader of the Content-Type of the response contains only application/json

    If I don't set the Content-Type I get a crash with error "HTTP 415 Unsupported Media Type"

     

    Regarding the loss of the json, running this script :

     

    qHttpRequest.SetHeader("Content-Type","application/json; charset=utf-8")

     

    changes the content of the response to "text/html;charset=utf-8"

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Are you able to use https://reqbin.com/ to test your API? If you are receiving the same (or similar) error, then this will indicate that either your POST message is not constructed correct or the server is doing something!

     

    Here's an example,

     

     

  • Thanks
    I can't try the proposed solution because we have a private website in http and not in https