cancel
Showing results for 
Search instead for 
Did you mean: 

Executing POST request using content type of x-www-form-urlencoded

KarimBalousha
Explorer
0 Kudos
446

I have an API that contains a request that uses x-www-form-urlencoded as body data type .. i noticed that when i use set_body_data the client proxy automaticly set the content-type to json so that won't work .. so i had to remove the request body before the execute and use the mo_http_client to set the custom content type and set_form_fields but that also didn't work .. any ideas how to send POST request with x-www-form-urlencoded data using client_proxy ?

as an example

data(lo_request) = mo_http_client->request.

lo_request->set_content_type( 'application/x-www-form-urlencoded' ).

lo_request->set_form_fields( value #( ( name = 'grant_type' value = 'password' ) ) ).

mo_client_proxy->create_resource( zaf_if_types=>gcs_resource_names-pets
)->create_request( /iwbep/if_v4_rest_types=>gcs_http_method-post
)->execute()

Notice : The used framework is iwbep client proxy rest

abap client proxy 
abap odata client proxy samples 

PRAGSMATIC
Participant
0 Kudos
Format the Body Data: Use a string to manually construct the x-www-form-urlencoded body since set_form_fields might not work as expected with IWBEP client proxy.

Accepted Solutions (0)

Answers (3)

Answers (3)

umasaral
Active Participant
0 Kudos

Hi KarimBalousha

Use mo_http_client and directly set the x-www-form-urlencoded data like this:
DATA(lo_request) = mo_http_client->request.

* Set the content type for x-www-form-urlencoded
lo_request->set_content_type( 'application/x-www-form-urlencoded' ).

* Prepare form fields
DATA: lt_form_fields TYPE TABLE OF string.
APPEND VALUE #( name = 'grant_type' value = 'password' ) TO lt_form_fields.
* Add other fields if needed

* Set form fields
lo_request->set_form_fields( lt_form_fields ).

* Execute the request
mo_http_client->create_resource( zaf_if_types=>gcs_resource_names-pets )
->create_request( /iwbep/if_v4_rest_types=>gcs_http_method-post )
->execute( ).
Ensure set_form_fields correctly formats the data for x-www-form-urlencoded.

 

umasaral
Active Participant
0 Kudos

Hi 

To send a POST request with x-www-form-urlencoded data using the iwbep client proxy, follow these steps:
Remove Default Request Body: Ensure no default body data is set before configuring the request.
Set Content Type and Form Fields:
DATA(lo_request) = mo_http_client->request.
lo_request->set_content_type( 'application/x-www-form-urlencoded' ).
lo_request->set_form_fields( VALUE #( ( name = 'grant_type' value = 'password' ) ) ).

Execute Request:
DATA(lo_response) = mo_client_proxy->create_resource( zaf_if_types=>gcs_resource_names-pets )
->create_request( /iwbep/if_v4_rest_types=>gcs_http_method-post )
->execute( ).
This approach should work if mo_http_client and mo_client_proxy are correctly initialized

Note:If you face issues, verify that the mo_http_client and mo_client_proxy are configured properly and that you are using the correct HTTP method and resource names.

KarimBalousha
Explorer
0 Kudos
Hi,
KarimBalousha
Explorer
0 Kudos

I've check every thing you've said, but still the execution somehow didn't include the fields that i've set in request using set_form_fields

PRAGSMATIC
Participant
0 Kudos

One example :

DATA: lv_form_data TYPE string,
lv_key TYPE string,
lv_value TYPE string.

lv_key = 'key1'.
lv_value = 'value1'.
lv_form_data = lv_key && '=' && cl_http_utility=>escape_url( lv_value ).

lv_key = 'key2'.
lv_value = 'value2'.
lv_form_data = lv_form_data && '&' && lv_key && '=' && cl_http_utility=>escape_url( lv_value ).

lo_http_client->request->set_cdata( lv_form_data ).

This method should be preferred over using set_body_data with x-www-form-urlencoded content, as set_body_data is typically used for raw data, and the content type can get overridden.

also
lo_http_client->request->set_header_field(
name = 'Content-Type'
value = 'application/x-www-form-urlencoded' ).

Ensure that all key-value pairs are properly URL-encoded.
Setting the content type to application/x-www-form-urlencoded is crucial, as the default content type might be JSON, which is why the previous approach might not have worked.

 

KarimBalousha
Explorer
0 Kudos
Hi, i've used set_form_fields with x-www-form-urlencoded not set_body, i've also tried the set_cdata approach that you've suggested but still the request don't contain any fields
PRAGSMATIC
Participant
0 Kudos
then you have to debug as to where it is not being set properly