cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI issue HTTP operation failed invoking https://xyz/graphql with statusCode: 400

former_member323756
Participant
0 Kudos
5,282

Hello All,

Our requirement is to query a GraphQL based api from CPI and push the response to C4C for ticket creation and customer creation.

So we are able to fetch the Bearer token for OAuth based authentication in CPI but when I try to push the same graphql request payload from CPI to the Rest API endpoint which is very much working and giving the response back from Postman, it gives the underlying error

"Error Details

org.apache.camel.component.ahc.AhcOperationFailedException: HTTP operation failed invoking https://xyz/graphql with statusCode: 400 "This API request in GraphQL would give a JSON response and the same can be handled by CPI using standard convertors available.

But the API not being able to comprehend my GraphQL request itself is the issue.

Update to clarify:

When I say that exactly the same payload worked with postman successfully but did not work with CPI HTTP adapter. The only difference there is as per my observation has been the radio button that we select to announce the request content type in POSTMAN.

I havnt found a way to tell my CPI editor that this is a GraphQL request payload that i am posting.

Not sure what header/parameter do I need to update to replicate this setting in CPI.

Note: I have maintained each and every header as postman in CPI too. But none states anything about GraphQL content type.

How should I handle the scenario here. Any suggestions ?

Regards,

Shikha

palash_mazumder
Participant
0 Kudos
It working with Content-Type = application/graphql
View Entire Topic
ErikSchouten
Newcomer
0 Kudos

I had the same issue. What I did was format the GraphQL as a JSON. Meaning for example if you have the following query: 

mutation Purchase_order_create {
    purchase_order_create(
        data: {
            po_date: "2024-09-05"
            subtotal: "0"
            shipping_price: "0"
            total_price: "0"
            po_number: "1234"
            warehouse_id: "azerty"
            line_items: [
                {
                    sku: "en-test-01"
                    expected_weight_in_lbs: "0"
                    quantity: 10
                    fulfillment_status: "Pending"
                    price: "0"
                }
            ]
        }
    ) {
        request_id
        complexity
    }
}

send it as: 

{
"query": "mutation Purchase_order_create { purchase_order_create( data: { po_date: \"2024-09-05\" subtotal: \"0\" shipping_price: \"0\" total_price: \"0\" po_number: \"1234\" warehouse_id: \"azerty\" line_items: [ { sku: \"en-test-01\" expected_weight_in_lbs: \"0\" quantity: 10 fulfillment_status: \"Pending\" price: \"0\" } ] } ) { request_id complexity } }"
}

Hope this helps. This way you can also leave the content-type 'application/json' as specified in the documentation. 

Here is the Groovy script I created that works for my queries. Your results may vary if you have more complicated queries. 

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonOutput

def processData(Message message) {
    // Get the body of the incoming message (which contains the GraphQL query)
    def graphqlQuery = message.getBody(String)
   
    // Convert the GraphQL query to the JSON format
    def jsonResult = convertGraphQLToJSON(graphqlQuery)
   
    // Convert the JSON object to a string
    def jsonString = JsonOutput.toJson(jsonResult)
   
    // Set the formatted JSON as the new message body
    message.setBody(jsonString)
   
    return message
}

def convertGraphQLToJSON(String graphqlQuery) {
    def jsonQuery = graphqlQuery.replaceAll('"', '\\"').replaceAll('\n', ' ')
    return [query: jsonQuery]
}