Child pages
  • Handle server-side form submitting in BSP
Skip to end of metadata
Go to start of metadata

How to handle server-side form submitting in BSP

Author: [~mp7hkab]
Submitted: 27 June 2008

About

This BSP snippet performs Form submitting with POST attributes to call a server page.
It then gets the response of the distant server and redirects the content to the client.

It's very useful when facing security matters, for instance when having to call a page with POST parameters including security parameters such as 'login' and 'password'...

Schema

Unknown macro: {gliffy}

Steps

  1. The browser sends a request to the SAP server
  2. The SAP server sends a request to the remote server. This request contains security informations, credentials...
  3. The remote server answers back to the SAP server, its response being some content (a PDF document for instance)
  4. The SAP server sends this content back to the user's browser, which will display the content accordingly to its MIME type (PDF,...)

Source code

DATA: http_client TYPE REF TO if_http_client.
DATA: form_data type tihttpnvp.
DATA: line type ihttpnvp.

* 1 - Create the html client

call method cl_http_client=>create_by_url
  exporting
    url    = 'http://my.url.tld:port/myfolder/mypage.jsp'
  importing
    client = http_client
  exceptions
    others = 1.

if sy-subrc = 1.
  ERROR_MSG = 'Others'.
endif.

* 2 - Set the method to 'POST'

call method http_client->request->set_header_field
  EXPORTING
    name  = '~request_method'
    value = 'POST'.

* 3 - Set the parameters

line-name = '_server'.
line-value = '10.01.01.01'.
append line to form_data.

line-name = '_user'.
line-value = 'myuser'.
append line to form_data.

line-name = '_password'.
line-value = 'mypwd'.
append line to form_data.

call method http_client->request->set_form_fields
  EXPORTING
    fields = form_data.

* 4 - Send the information

call method http_client->send
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2.

if sy-subrc = 1.
  ERROR_MSG = 'Communication Failure'.
elseif sy-subrc = 2.
  ERROR_MSG = 'HTTP Invalid State'.
endif.

* 5 - Get the result back

call method http_client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3.

if sy-subrc = 1.
  ERROR_MSG = 'Communication Failure'.
elseif sy-subrc = 2.
  ERROR_MSG = 'HTTP Invalid State'.
elseif sy-subrc = 3.
  ERROR_MSG = 'HTTP Processing Failed'.
endif.

* 6 - Get the content of the returned page

DATA: page_content type xstring.
page_content = http_client->response->get_data( ).

* 7 - Create the returned response (PDF document)

DATA: response type ref to if_http_response.
response = runtime->server->response.

response->delete_header_field( name = 'Cache-Control' ).
response->delete_header_field( name = 'Expires' ).
response->delete_header_field( name = 'Pragma' ).

call method response->set_content_type
  EXPORTING
    content_type = 'application/pdf'.

call method response->set_data
  EXPORTING
    data = page_content.