cancel
Showing results for 
Search instead for 
Did you mean: 

Consuming .NET REST web service in CRM

former_member183993
Participant
0 Kudos

hi experts,

I am using CRM 7.0 EHP1 and trying to consume a REST web service to post data to our .NET app after specific processes in CRM.  Basically an indicator that something took place in CRM.

There is no WSDL to consume through service consumer.  I have found several posts regarding this and nothing provided seems to work. 

If anyone has any how to's on this, please send my way. 

I have tried some test programs using the code below without success.

CALL METHOD cl_http_client=>create_by_url

   EXPORTING

     url    = url

   IMPORTING

     client = lr_client.

CALL METHOD lr_client->request->set_method

   EXPORTING

     method = 'POST'.

CONCATENATE url '/?'

'"AdditionalInfo":"String content","Alias":"String content","ContactPersonId":"1111111","Email":"testankit20130408.1@mailinator.com","MarketingProspectId":"0000290247","ReasonToPromoteCode":0'

INTO url2.

cl_http_utility=>set_request_uri( request = lr_client->request uri = url2 ).

CALL METHOD lr_client->send

   EXCEPTIONS

     http_communication_failure = 1

     http_invalid_state         = 2

     http_processing_failed     = 3

     http_invalid_timeout       = 4

     OTHERS                     = 5.


Accepted Solutions (0)

Answers (1)

Answers (1)

former_member197445
Contributor
0 Kudos

You might get more help in the ABAP forums, since this is more of an ABAP question than a .NET question.

Having said that, there are two things I notice about your code.

  1. The json is not formed correctly.  You need the curly brackets.
    CONCATENATE '{"AdditionalInfo":"String content","Alias":"String content","ContactPersonId":"1111111"}' etc.
  2. I don't think you can pass the json in the url.  It needs to be in the BODY of the http request.

This sample code is not a post with JSON, but it definitely works.  Hope it helps.

*&---------------------------------------------------------------------*
*& Report  Z_REST_CALL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  Z_REST_CALL.


*&---------------------------------------------------------------------*
*&      Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .

*&---------------------------------------------------------------------*
*&      Types and Data
*&---------------------------------------------------------------------*
DATAhttp_client    TYPE REF TO if_http_client ,
http_url       TYPE string                ,
p_content      TYPE string                .

*&---------------------------------------------------------------------*
*&      Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .

* Build the url string based on input
CONCATENATE 'http://www.webservicex.net/globalweather.asmx'
'/GetCitiesByCountry?CountryName='
p_cnt
INTO http_url .

* Creation of new IF_HTTP_Client object
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url                = http_url
IMPORTING
client             = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active  = 2
internal_error     = 3
OTHERS             = 4.

http_client->request->set_header_field( name  = '~request_method'
value = 'GET' ).
* Send the request
http_client->send( ).

* Reterive the result
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state         = 2
http_processing_failed     = 3
OTHERS                     = 4.

p_content = http_client->response->get_cdata( ).
REPLACE  ALL OCCURRENCES OF '&lt;' IN p_content WITH '<' .
REPLACE ALL OCCURRENCES OF '&gt;' IN p_content WITH '>' .

*&---------------------------------------------------------------------*
*&      Processing the string
*&---------------------------------------------------------------------*
DATA : moff  TYPE syst-tabix ,
moff1 TYPE syst-tabix ,
len   TYPE syst-tabix .

DO .
   FIND '<City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
  IF sy-subrc = 0 .
    moff = moff + 6 .
    FIND '</City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
    len = moff1 - moff .
    WRITE : / p_content+moff(len) .
  ELSE.
    EXIT.
  ENDIF.

ENDDO .