Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Laura Spinelli
    curl request to OIC not workingAnswered
    Topic posted August 2, 2019 by Laura SpinelliGreen Ribbon: 100+ Points 
    19 Views, 4 Comments
    Title:
    curl request to OIC not working
    Content:

    Hello guys,

    I have a custom process that makes a curl request to OIC. I am using POST method. I get a response of 1 but the request never gets to OIC (when can check it on the OIC log).

    When I use GET method or call it from a javascript add-in and even when I test it on postman, the request works - we can see it on the OIC log and we receive the expected response. 

    My OSvC version is 19B. 

    Is there any configuration I should enable to use this? 

    I have attached part of my code bellow (only had to hide part of our url).

    Thanks for the help!

    Code Snippet:

    Best Comment

    Robert Surujbhan

    Hi Laura - no special configurations to enable.  A few observations based on your code snippet:

    1. You will likely need to run json_encode() on your $fields array when sending the data into CURLOPT_POSTFIELDS
    2. You should use CURLOPT_HTTPHEADER to set the 'Content-Type' and 'Accept' headers to 'application/json' (if you aren't doing this, OIC might not process the request due to an invalid/missing media type on the request)
    3. You may also need to pass the 'Authorization' header with credentials depending on how you've configured security on the OIC integration (didn't see this in your code, but it might be there?)
    4. In your curl code, run/inspect curl_getinfo($ch) and also check for anything in curl_error($ch) after curl_exec($ch) runs, and see what these contain - might shed some light into the problem

    Comment

     

    • Robert Surujbhan

      Hi Laura - no special configurations to enable.  A few observations based on your code snippet:

      1. You will likely need to run json_encode() on your $fields array when sending the data into CURLOPT_POSTFIELDS
      2. You should use CURLOPT_HTTPHEADER to set the 'Content-Type' and 'Accept' headers to 'application/json' (if you aren't doing this, OIC might not process the request due to an invalid/missing media type on the request)
      3. You may also need to pass the 'Authorization' header with credentials depending on how you've configured security on the OIC integration (didn't see this in your code, but it might be there?)
      4. In your curl code, run/inspect curl_getinfo($ch) and also check for anything in curl_error($ch) after curl_exec($ch) runs, and see what these contain - might shed some light into the problem
      • Laura Spinelli

        Hi Robert,

        Thanks for the help and sorry for the delay in responding. 

        I added the json_encode and a header to the request and it is now leaving RightNow. OIC returns the following error:

        "title" : "Fault Details : \n<nsmpr1:APIInvocationError xmlns:nsmpr1=\"http://xmlns.oracle.com/cloud/generic/rest/fault\"><nsmpr1:type/><nsmpr1:title/><nsmpr1:detail/><nsmpr1:errorCode/><nsmpr1:errorDetails><nsmpr1:type/><nsmpr1:title>Non-http status code</nsmpr1:title><nsmpr1:errorCode>REST_REQ_PLD_ERR</nsmpr1:errorCode><nsmpr1:errorPath>&lt;![CDATA[An error occurred while processing the request payload in the target REST endpoint.]]&gt;</nsmpr1:errorPath><nsmpr1:instance>&lt;![CDATA[Target REST endpoint request payload processing failed.[[ORABPEL-15236\n\nexception.15236.name\nexception.15236.description\nexception.15236.fix\n]]]]&gt;</nsmpr1:instance></nsmpr1:errorDetails></nsmpr1:APIInvocationError>"

        My header is like this: 

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json','Content-Length: ' . strlen($fields)));

        I tried sending the authorization on the header and using CURLOPT_USERPWD, and both return the same response.

        On curl_getinfo it shows null for content-type and 0 for header-size. I would expect these parameters to be filled with the values I am sending, am I wrong? 

        • Robert Surujbhan

          Hi Laura, those values in curl_getinfo represent what the server sent back as a response, not what you sent in your request (e.g., a null for content-type means the server did not send back a valid Content-Type header)

          I would recommend two things to check next.

          1. The Content-Length header - this needs to be a strlen() on the $fields array after running json_encode() on it (a string), and not just a strlen() on the $fields PHP array (an object).  In your last code snippet, I can't tell if you are overwriting $fields with json_encode or not?  For testing, you can also just remove the Content-Length header for now (it's usually not required to be set explicitly)
          2. In OIC, when you visit the /metadata page in your web browser for your integration (/ic/api/integration/v1/flows/rest/TEST_OSVC_TO_OCC_CREATE/1.0/createproduct/metadata), you should see a "Resource" section on that page that gives you a request sample.  In your PHP code, test the json_encode() string output on your $fields array - do the json payloads match?