function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siva Ramesh 1Siva Ramesh 1 

Einstein Language Intent - 400 Bad Request

I have been trying to use the Intent Web Service and I am able to get a 200 and Response when I POST from cURL and SOAPUI. However, when I construct the request body in APEX and POST am getting a Error - 400 which is a generic BAD request response. I have inspected the requests from cURL and my Apex anonymous code and both are identical. Not sure why I am getting this error. With limited error message details (since Language is still in Beta) am unable to proceed. Below are screenshots from requestbin -

POST from Salesforce -

User-added image

POST from cURL -

User-added image
Michael Machado 22Michael Machado 22

Can you try again? We just made a change that should address any outstanding issues.
 

Thanks!

Siva Ramesh 1Siva Ramesh 1
Thats what I thought. I was getting a 300 error and nugenix server restart. Will try and advise! Regards, Siva Ramesh
Siva Ramesh 1Siva Ramesh 1
Am getting a Status=Unauthorized, StatusCode=401 - Invalid authentication scheme. I generated a new token to ensure that is not a issue
Siva Ramesh 1Siva Ramesh 1
Here is the error when I try posting using cURL
User-added image
JeanMarc SoumetJeanMarc Soumet
Siva what is the url you are using in your cURL call?
Can you provide the API call without your oauth token?
 
Siva Ramesh 1Siva Ramesh 1
JeanMarc,

Am using the https://api.einstein.ai/v2/language/intent and am getting a failed to connect to Port 80 exception while trying to POST from cURL. 

Below is the API call -

Http h = new Http();
    
     // Instantiate a new HTTP request
    HttpRequest req = new HttpRequest();
   
    req.setMethod('POST');
    // Send the request, and return a response
    String contentBoundary =  HttpFormBuilder.WriteBoundary();
 
    String modelId = ''; //insert your model Id
    String sampleId= 'test';
 
  
  req.setEndpoint('https://api.einstein.ai/v2/language/intent');

    
     
   String content = 'how is my package being shipped?';
   String contentType = '';//'\nContent-Type: text/plain\nContent-Transfer-Encoding: gzip';
   String token = 'Insert the token here';
// assemble the body payload
    String headerValue = '----' + contentBoundary  + contentType+ '\nContent-Disposition: form-data; name="modelId";\n\n'  + modelId + '\n';
    String body =  '----' + contentBoundary +contentType+ '\nContent-Disposition: form-data; name="document";\n\n'  + content  + '\n';
    String bodySample = '----' + contentBoundary + '\nContent-Disposition: form-data; name="sampleId";\n\n'  + 'Test'  + '\n';
    String footer ='----'+ contentBoundary + '--' ;
    String bodyPayload = headerValue + body + footer; 


     String authorizationHeader = 'Bearer ' + token;
     //EncodingUtil.base64Encode(headerValue);
     req.setHeader('Content-Type', 'multipart/form-data; boundary=' + '--'+ contentBoundary);
     req.setHeader('Authorization', authorizationHeader);
     req.setHeader('Cache-Control', 'no-cache');
    //if the request and response for your REST call is JSON, use the code below.
     req.setHeader('accept', '*/*');
     req.setHeader('Compressed', 'true');
    // system.debug('the body of the request is '+bodyPayLoad);
     
     //req.setHeader('Accept', '*/*');
    req.setBody(bodyPayload);
    system.debug('request is'+req.getBody());
    HttpResponse res = h.send(req);
    system.debug('the response is '+ res.getBody());
JeanMarc SoumetJeanMarc Soumet
This looks more like Apex code. I just added recently some util classes for Apex. Can you try to use from https://github.com/MetaMind/apex-utils