GETRESPONSEBODYASSTRING Method

Returns the entire body from the HTTP response in character string format.

Requirement:A character set for the response body must not be set after getting the response body. A character set for the response body must be specified with the SETRESPONSEBODYCHARACTERSET method before getting the response body with the GETRESPONSEBODYASSTRING method.

Syntax

package.GETRESPONSEBODYASSTRING(variable, rc);

Required Arguments

package

specifies an instance of the HTTP package variable.

variable

specifies the variable to hold the entire response body.

rc

specifies the variable to hold the return code value.

Note: A return code value of 0 indicates success; a value of 1 indicates failure.

Details

Use the GETRESPONSEBODYASSTRING method to retrieve the response body in character string format. The response body is returned as one entity.

If the content type is not set, the HTTP client deduces a character set from the response content type. If the HTTP client is unable to deduce a character set from the response content type, the character set defaults to UTF-8. However, you can use the SETRESPONSEBODYCHARACTERSET method to specify the character set to use when retrieving the response body.

You can call the GETRESPONSEBODYASSTRING method after using the EXECUTEMETHOD method to retrieve the response body returned by the web server.

Note: The GETREAPONSEBODYASSTRING method does not return until the web client has received all the data from the web server.
Note: The response body is transcoded to the encoding of the string if the encodings are different.
Note: The EXECUTEMETHOD method does not support streaming of the response body.

Example

data _null_;
  method init();
    declare package http h();
    declare varchar(1024) character set utf8 body;
    declare int rc status;
    declare char(2) code;
 
    /* Build and send a GET method for a resource */
    code = 'FR';
    h.createGetMethod('http://api.worldbank.org/countries/'
      || code || '/indicators/NY.GNP.PCAP.CD/?date=1990:1991');
    h.executeMethod();

    /* If the resource was returned by the server, show the response */
    status = h.getStatusCode();   /* get the HTTP status code */
    put 'Requested resource for country code:' code 'executeMethod() status:' status;
    if status eq 200 then do;     /* 200 = OK */      
      /* retrieve the body from the response that came from the server */
      h.getResponseBodyAsString(body, rc);
      put 'Body:'; 
      put body;
    end;
  end;
enddata; run;

The following lines are written to the log.

Requested resource for country code: FR executeMethod() status: 200
Body:
<?xml version="1.0" encoding="utf-8"?>
<wb:data page="1" pages="1" per_page="50" total="2"
xmlns:wb="http://www.worldbank.org">
  <wb:data>
    <wb:indicator id="NY.GNP.PCAP.CD">GNI per
capita, Atlas method (current US$)</wb:indicator>
    <wb:country id="FR">France</wb:country>
   <wb:date>1991</wb:date>
    <wb:value>20880</wb:value>
    <wb:decimal>0</wb:decimal>
</wb:data>
  <wb:data>
    <wb:indicator id="NY.GNP.PCAP.CD">GNI per capita, Atlas method
(current US$)</wb:indicator>
    <wb:country id="FR">France</wb:country>
<wb:date>1990</wb:date>
    <wb:value>20050</wb:value>
    <wb:decimal>0</wb:decimal>
</wb:data>
</wb:data>

See Also

Last updated: June 2, 2025