Skip to Content
0

Adding a cookie to an http request

Apr 13 at 12:15 PM

24

avatar image

Hi all,

I need to do a http request adding a specific cookie. It's working fine except the cookie part. It seems the cookie is not being sent according to the responde I'm getting from the server.

This is the piece of code to create the http client:

CALL METHOD cl_http_client=>create_by_url EXPORTING
url ='url'

IMPORTING client = http_client

EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3OTHERS = 4.

I have used several pieces of code to add the cookie but unfortunately no success:

http_client->request->set_cookie( name = 'JSESSIONID'

value = l_session_id ).


CALL METHOD http_client->request->set_cookie EXPORTING
name = 'JSESSIONID' value =

l_session_id.


DATA: l_cookie_value TYPE string.

CONCATENATE 'JSESSIONID=' l_session_id INTO
l_cookie_value.

CALL METHOD http_client->request->set_header_field EXPORTING
name = 'Cookie' value = l_cookie_value.

Any ideas what's wrong with my code?

Regards,

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Nabheet Madan Apr 13 at 12:25 PM
0

I assume you are making an API call for something like POST/PATCH. If this is the case then first you will need to fetch XSRF token along with it you will get the cookie, use the object to make further POST/PATCH calls Please find below sample code taken from API Hub for a PATCH Request

DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string.


"create HTTP client by url
"API endpoint for API sandbox 
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = 'https://sandbox.api.sap.com/workflow-service/rest/v1/task-instances/{taskInstanceId}'


  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.


"Available API Endpoints
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.ap1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.br1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.ca1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.cn1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.eu3.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.hanatrial.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.jp1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.ru1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.us1.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.us2.hana.ondemand.com/workflow-service/rest
"https://bpmworkflowruntime{provideraccountname}-{consumeraccountname}.us3.hana.ondemand.com/workflow-service/rest


IF sy-subrc <> 0.
  "error handling
ENDIF.


"setting request method
lo_http_client->request->set_method('PATCH').


"adding headers
lo_http_client->request->set_header_field( name = 'X-CSRF-Token' value = 'string' ).
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
"API Key for API Sandbox
lo_http_client->request->set_header_field( name = 'APIKey' value = '<API_KEY>' ).




"Available Security Schemes for productive API Endpoints
"Basic Authentication, OAuth 2.0


"Basic Auth : provide username:password in Base64 encoded in Authorization header
"lo_http_client->request->set_header_field( name = 'Authorization' value = 'Basic <Base64 encoded value>' ).


CALL METHOD lo_http_client->request->set_cdata
  EXPORTING
    data = '{  "context": {},  "status": "COMPLETED",  "subject": "string",  "description": "string",  "recipientUsers": "string",  "recipientGroups": "string",  "processor": "string",  "dueDate": "2018-04-13T12:22:35.793Z",  "priority": "VERY_HIGH"}'.


CALL METHOD 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.
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 5.
ENDIF.


IF sy-subrc <> 0.
  "error handling
ENDIF.


response = lo_http_client->response->get_cdata( ).


WRITE: 'response: ', response.
Show 3 Share
10 |10000 characters needed characters left characters exceeded

Thanks Nabheet Madan but I'm executing a POST method in fact it's a soap web service for which we're having problems to do it the clean way by using abap proxies that's why we resorted to do a htttp call.

Any ideas what it's wrong with my code?

0

Just before executing the http request I have cheched the cookie is correctly set with this piece of code:

DATA: l_t_cookies TYPE tihttpcki,
l_wa_cookie TYPE line of tihttpcki.

CALL METHOD lo_http_client->request->get_cookies CHANGING

cookies = l_t_cookies.

LOOP AT l_t_cookies INTO l_wa_cookie.

WRITE:/ sy-tabix, l_wa_cookie-name, l_wa_cookie-value.

ENDLOOP.

Any ideas why it seems the cookie is not getting the server.

Regards,

0
Daniel González Suárez

Please paste your complete code where you get the cookie and then you pass back

0
Daniel González Suárez Apr 17 at 07:27 AM
0

Hi all,

My problem is now solved. It turns out the cookie name has the character "." and SAP is encoding/replacing the "." character by "%2e". As a result the server is not understanding the cookie name.

To avoid the encoding, just apply SAP NOTE 1160362 to set the parameter "ict/disable_cookie_urlencoding=1".

Regards,

Share
10 |10000 characters needed characters left characters exceeded