cancel
Showing results for 
Search instead for 
Did you mean: 

Send .xlsx document using CL_HTTP_CLIENT

srikanthnalluri
Active Participant
1,274

Hi,

We're trying to send the .xlsx file using the CL_HTTP_CLIENT but getting an error 'Bad Request, the .xlsx data is in xstring format.

But when we try to post the data using postman it is getting uploaded, but this data is in binary format. How do we send binary data table using CL_HTTP_CLIENT. Can you please help me out to send the binary data. Appreciate your help. Thank you

  data:
    lv_url           type string,
    lv_token_bearer  type string,
    lv_response      type string,
    lo_http_client   type ref to if_http_client.

  check ls_token is not initial.
  lv_url = |https://xxxxxxx.sharepoint.com/sites/SiteName/| &&
           |_api/web/GetFolderByServerRelativeUrl('Shared Documents')/| && 
           |Files/add(url='Test1.xlsx',overwrite=true)|.

  call method cl_http_client=>create_by_url
    exporting
      url                = lv_url
    importing
      client             = lo_http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.
  if sy-subrc <> 0.
*   raise exception.
  endif.

  lv_token_bearer = |Bearer | && ls_token-access_token.

  lo_http_client->request->set_header_field(
    name  = 'Accept'                                        "#EC NOTEXT
    value = 'application/json;odata=verbose' ).

  lo_http_client->request->set_header_field(
    name  = 'Content-Type'                                  "#EC NOTEXT
    value = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ).

  data: lv_content_length type string.
  lv_content_length = xstrlen( xstring_data ).
  lo_http_client->request->set_header_field(
    name  = 'Content-Length'                                "#EC NOTEXT
    value = lv_content_length ).

  lo_http_client->request->set_header_field(
    name  = 'Authorization'                                 "#EC NOTEXT
    value = lv_token_bearer ).

  lo_http_client->request->set_method('POST').

  lo_http_client->request->set_data(
    data = xstring_data ).
lo_http_client->send( exceptions http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 others = 5 ). if sy-subrc = 0. lo_http_client->receive( exceptions http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 others = 4 ). endif. lv_response = lo_http_client->response->get_cdata( ). data: lv_code type i, lv_reason type string. lo_http_client->response->get_status( importing code = lv_code reason = lv_reason ). lo_http_client->close( exceptions http_invalid_state = 1 others = 2 ). if lv_code ne 200. * Raise Exception endif.
Former Member

How does your code look like?

What kind of error is it? Is it returned by the sever?

Sandra_Rossi
Active Contributor

I think that bad request can mean anything wrong in your request, so you should post your code.

For information, it's very simple for sending xstring containing Excel file in XLSX true format, I guess it's what you did, so better include in your question so that we just confirm that it's good:

request->set_data( xstring_xlsx ).
request->set_content_type( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ).
srikanthnalluri
Active Participant
0 Kudos

gabmarian, sandra.rossi

Appreciate your comment. I have include the test code in the question, the error is from the server. Postman is loading the data in Binary format to process the request, is there way to send the binary table using CL_HTTP_CLIENT. Thank you

Sandra_Rossi
Active Contributor
0 Kudos

Sorry, made a mistake in my previous comment. I replaced client-> with request->.

srikanthnalluri
Active Participant
0 Kudos

Any way we can send the binary table data using CL_HTTP_CLIENT?

Tomas_Buryanek
Active Contributor
0 Kudos

CL_HTTP_CLIENT accepts only xstring. You should convert binary table to xstring. Maybe you have mistake in this conversion? Are you giving correct binary data length to conversion?

EDIT: Few tips - check response header fields to better understand where is the issue. Why is webservice returning error? There is only "Bad request" text in body?

Former Member

How do you generate the .xlsx file you are sending?

srikanthnalluri
Active Participant
0 Kudos

tomas.buryanek, I tried with correct length of the xstring. The body has only this information " 400 Bad HTTP request####0001ae4a"

Is there any way we can place the excel file.( This xstring is generated using abap2xlsx)

Tomas_Buryanek
Active Contributor
0 Kudos

Are there any details in HTTP response headers? In successful response in postman you can see there are 29 header fields. You can get them with method response->get_header_fields( ).

Former Member
0 Kudos

Are you sure that the .xlsx file is correct? If you download it from SAPGUI to your desktop will it open in Excel without any issues?

srikanthnalluri
Active Participant
0 Kudos

tomas.buryanek, There is not much information in the response header. Please check below response header details.

gabmarian, This file is generated using abap2xlsx project. We tested downloading it into our local machine and it is working.

Tomas_Buryanek
Active Contributor
0 Kudos

srikanthnalluri Thanks for header fields. I am not 100% sure about this, but you can see you are getting Bad Request from server SAP NetWeaver (it seems). Not from the server you are calling - Sharepoint. That can mean that connection to that outside Sharepoint server did not went through... Try check with Basis or analyse some HTTP logs and analytic t-codes for HTTP connection, trace... (can not help much in this area, sorry).

Your code looks OK and XLSX files is also OK as you tested it on local machine. So it points to HTTP connection from SAP server to outside.

Accepted Solutions (0)

Answers (0)