<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: ABAP HTTP Post Request in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353949#M4626355</link>
    <description>&lt;P&gt;The request is exactly the Same in Postman.&lt;/P&gt;&lt;P&gt;First I make the Get in Postman with a fetch and then I used the received Token for the Post.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In my Code I set the Code in the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;lo_client-&amp;gt;request-&amp;gt;set_header_field(

EXPORTING

name = 'X-CSRF-Token' " Name of the header field

value = lv_token ).&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 15 Jan 2021 15:55:00 GMT</pubDate>
    <dc:creator>tobias96</dc:creator>
    <dc:date>2021-01-15T15:55:00Z</dc:date>
    <item>
      <title>ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaq-p/12353947</link>
      <description>&lt;P&gt;Hello everybody,&lt;/P&gt;
  &lt;P&gt;I try to make a Post to a API URL which works perfect with Postman.&lt;/P&gt;
  &lt;P&gt;First I make the GET to Fetch the X-CSRF-Token, this works but if I try to copy this Token in the Header of the Post Request, I get a 403 Error as status_code.&lt;/P&gt;
  &lt;P&gt;I try the following Code:&lt;/P&gt;
  &lt;P&gt;&lt;BR /&gt;&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;REPORT YTT_TEST.

*----------------------------------------------------------------------*
*Selection-Screen
*----------------------------------------------------------------------*
PARAMETERS: p_name TYPE string DEFAULT 'TEST'.
PARAMETERS: p_pass TYPE string DEFAULT 'TEST123' LOWER CASE.



DATA lo_client TYPE REF TO if_http_client.

DATA lo_response TYPE REF TO if_rest_entity.

DATA lv_response TYPE string.

DATA lv_token TYPE string.

DATA lv_session TYPE string.

DATA lv_xcrf TYPE string.

DATA lv_http_status TYPE string.

DATA gc_content_type_form TYPE string VALUE 'application/json; charset=utf-8'.

DATA gc_url TYPE string VALUE &amp;lt;URL&amp;gt;.



*----------------------------------------------------------------------*
*At Selection Screen Output
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'P_PASS'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.

"======================Getting CSRF token ==========================================

START-OF-SELECTION.

cl_http_client=&amp;gt;create_by_url(

EXPORTING

url = gc_url

IMPORTING

client = lo_client

EXCEPTIONS

OTHERS = 4 ).


lo_client-&amp;gt;authenticate( username = p_name
password = p_pass ).

lo_client-&amp;gt;request-&amp;gt;set_content_type( content_type = gc_content_type_form ).



lo_client-&amp;gt;request-&amp;gt;set_method( if_http_request=&amp;gt;co_request_method_get ).




lo_client-&amp;gt;request-&amp;gt;set_header_field(

EXPORTING

name = 'X-CSRF-Token' " Name of the header field

value = 'Fetch' ).



lo_client-&amp;gt;send(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

http_invalid_timeout = 4

OTHERS = 5 ).





lo_client-&amp;gt;receive(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ).





lv_token = lo_client-&amp;gt;response-&amp;gt;get_header_field('X-CSRF-Token').

lv_session = lo_client-&amp;gt;response-&amp;gt;get_header_field('set-cookie').



lo_client-&amp;gt;close( ).

FREE lo_client.

"===========================end of Getting CSRF token ====================================





"=========================validation CSRF token with Post request=========================



cl_http_client=&amp;gt;create_by_url(

EXPORTING

url = gc_url

IMPORTING

client = lo_client

EXCEPTIONS

OTHERS = 4 ).

lo_client-&amp;gt;authenticate( username = p_name
password = p_pass ).

lo_client-&amp;gt;request-&amp;gt;set_content_type( content_type = gc_content_type_form ).



lo_client-&amp;gt;request-&amp;gt;set_method( if_http_request=&amp;gt;co_request_method_post ).


lo_client-&amp;gt;request-&amp;gt;set_header_field(

EXPORTING

name = 'X-CSRF-Token' " Name of the header field

value = lv_token ).

*lv_xcrf = lo_client-&amp;gt;request-&amp;gt;get_header_field('X-CSRF-Token' ).

lo_client-&amp;gt;request-&amp;gt;set_form_field(

EXPORTING

name = 'Cookie' " Name of form field

value = lv_session ).





lo_client-&amp;gt;send(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

http_invalid_timeout = 4

OTHERS = 5

).



lo_client-&amp;gt;receive(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ).



lv_http_status = lo_client-&amp;gt;response-&amp;gt;get_header_field( '~status_code' ).

lv_response = lo_client-&amp;gt;response-&amp;gt;get_header_field('~status_reason' ).

lv_xcrf = lo_client-&amp;gt;response-&amp;gt;get_header_field('x-csrf-token' ).



WRITE :/ 'Status:', lv_http_status.

WRITE :/ 'Response:', lv_response.

WRITE :/ 'CSRF-Token:', lv_xcrf .
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Jan 2021 13:51:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaq-p/12353947</guid>
      <dc:creator>tobias96</dc:creator>
      <dc:date>2021-01-15T13:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353948#M4626354</link>
      <description>&lt;P&gt;Is your second ABAP request really exactly the same like successful test in Postman? Is not there something you missed? Are you putting token correctly to header fileds and "Cookie" as form field?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jan 2021 14:51:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353948#M4626354</guid>
      <dc:creator>Tomas_Buryanek</dc:creator>
      <dc:date>2021-01-15T14:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353949#M4626355</link>
      <description>&lt;P&gt;The request is exactly the Same in Postman.&lt;/P&gt;&lt;P&gt;First I make the Get in Postman with a fetch and then I used the received Token for the Post.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In my Code I set the Code in the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;lo_client-&amp;gt;request-&amp;gt;set_header_field(

EXPORTING

name = 'X-CSRF-Token' " Name of the header field

value = lv_token ).&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Jan 2021 15:55:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353949#M4626355</guid>
      <dc:creator>tobias96</dc:creator>
      <dc:date>2021-01-15T15:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353950#M4626356</link>
      <description>&lt;P&gt;Hello  &lt;SPAN class="mention-scrubbed"&gt;tobias96&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Here is a very similar question. The solution was to set AcceptCooki property to enabled.&lt;/P&gt;&lt;P&gt;&lt;A href="https://answers.sap.com/answers/12899029/view.html" target="test_blank"&gt;https://answers.sap.com/answers/12899029/view.html&lt;/A&gt;&lt;/P&gt;Kind regards,&lt;BR /&gt;Mateusz</description>
      <pubDate>Fri, 15 Jan 2021 16:17:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353950#M4626356</guid>
      <dc:creator>MateuszAdamus</dc:creator>
      <dc:date>2021-01-15T16:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353951#M4626357</link>
      <description>&lt;P&gt;Sadly that does not work for me.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jan 2021 18:09:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353951#M4626357</guid>
      <dc:creator>tobias96</dc:creator>
      <dc:date>2021-01-15T18:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP Post Request</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353952#M4626358</link>
      <description>&lt;P&gt;OK now I solved it. The solution was only open once the create by url and only once authenticate myself.&lt;/P&gt;&lt;P&gt;Here is my solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;REPORT ytt_post_req_2.


DATA lo_client                TYPE REF TO       if_http_client.


DATA lo_response              TYPE REF TO       if_rest_entity.


DATA lv_response              TYPE string.


DATA lv_token                 TYPE string.


DATA lv_session               TYPE string.


DATA lv_xcrf                  TYPE string.


DATA lv_http_status           TYPE string.


DATA  gc_content_type_form    TYPE string VALUE 'application/json; charset=utf-8'.


DATA  gc_url                  TYPE string VALUE 'URL'.










"======================Getting CSRF token ==========================================






cl_http_client=&amp;gt;create_by_url(


    EXPORTING


      url     = gc_url


    IMPORTING


      client  = lo_client


    EXCEPTIONS


      OTHERS  = 4 ).






lo_client-&amp;gt;request-&amp;gt;set_content_type( content_type = gc_content_type_form ).






lo_client-&amp;gt;request-&amp;gt;set_method( if_http_request=&amp;gt;co_request_method_get ).






*lo_client-&amp;gt;authenticate(
*
*
*  EXPORTING
*
*
*
*    username             =  ''               " ABAP System, User Logon Name
*
*
*    password             =  ''                " Logon ID
*
*).              " SAP System, Current Language


lo_client-&amp;gt;propertytype_accept_cookie = if_http_client=&amp;gt;co_enabled.




lo_client-&amp;gt;request-&amp;gt;set_header_field(


  EXPORTING


    name  =  'X-CSRF-Token'                " Name of the header field


    value =  'Fetch'   ).






lo_client-&amp;gt;send(


      EXCEPTIONS


        http_communication_failure = 1


        http_invalid_state         = 2


        http_processing_failed     = 3


        http_invalid_timeout       = 4


        OTHERS                     = 5  ).










lo_client-&amp;gt;receive(


  EXCEPTIONS


    http_communication_failure = 1


    http_invalid_state         = 2


    http_processing_failed     = 3


    OTHERS                     = 4 ).










lv_token    = lo_client-&amp;gt;response-&amp;gt;get_header_field('X-CSRF-Token').


lv_session  = lo_client-&amp;gt;response-&amp;gt;get_header_field('set-cookie').










*"===========================end of Getting CSRF token =========================================


*


*


*


*


*"=========================validation CSRF token with Post request==============================


*






*


lo_client-&amp;gt;request-&amp;gt;set_method( if_http_request=&amp;gt;co_request_method_post ).






lo_client-&amp;gt;request-&amp;gt;set_header_field(


  EXPORTING


    name  = 'X-CSRF-Token'                " Name of the header field


    value =    lv_token  ).






lo_client-&amp;gt;request-&amp;gt;set_form_field(


  EXPORTING


    name  = 'Cookie'                    " Name of form field


    value =   lv_session   ).










lo_client-&amp;gt;send(


      EXCEPTIONS


        http_communication_failure = 1


        http_invalid_state         = 2


        http_processing_failed     = 3


        http_invalid_timeout       = 4


        OTHERS                     = 5


    ).






lo_client-&amp;gt;receive(


  EXCEPTIONS


    http_communication_failure = 1


    http_invalid_state         = 2


    http_processing_failed     = 3


    OTHERS                     = 4 ).






lv_http_status = lo_client-&amp;gt;response-&amp;gt;get_header_field( '~status_code' ).


lv_response    = lo_client-&amp;gt;response-&amp;gt;get_header_field('~status_reason' ).


lv_xcrf        = lo_client-&amp;gt;response-&amp;gt;get_header_field('x-csrf-token' ).






WRITE :/ 'Satus:', lv_http_status.


WRITE :/ 'Response:', lv_response.


WRITE :/ 'CSRF-Token:', lv_xcrf.

DATA(lv_result) = lo_client-&amp;gt;response-&amp;gt;get_cdata( ).
WRITE: / lv_result.&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jan 2021 12:12:50 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-http-post-request/qaa-p/12353952#M4626358</guid>
      <dc:creator>tobias96</dc:creator>
      <dc:date>2021-01-18T12:12:50Z</dc:date>
    </item>
  </channel>
</rss>

