Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, windows and java based applications. Get your 30 day free trial.

Discuss your Sahi Pro usage patterns, best practices, problems and solutions. Help others solve their problems and seek help from the community when needed. If you need specific support on your application, please email support @ sahipro.com

Request API with file

Hi,

I am trying to automate a test for an api endpoint that requires to upload a file. Here's the cURL for the request:

cURL --location 'http://localhost/bt_main/api/label_analyzer/analyze_image?include_work_product=true' \
--header 'x-bt-client-id: API_TEST_CLIENT' \
--header 'x-bt-client-instance-id: *' \
--header 'x-bt-client-version: 1.14.0' \
--header 'x-bt-auth: \
--header 'Cookie: JSESSIONID=54D4DB7529E99AD5C02FA9B8C774335B' \
--form 'file=@/C:/Users/karlo/sahipro/scripts/BearTracks/data/001.jpg'

On Sahi Pro, the generated script is this:

$request = new RESTRequest();
$request.setURL("https://localhost/bt_main/api/label_analyzer/analyze_image");
$request.addToQueryString("include_work_product", "true");
$request.setHeader("x-bt-client-id", "API_TEST_CLIENT");
$request.setHeader("x-bt-client-instance-id", "*");
$request.setHeader("x-bt-client-version", "1.14.0");
$request.setHeader("x-bt-auth", "");
$request.setHeader("Content-Type", "multipart/form-data");
$request.addToBody("file", "sahi-file://C:/Users/karlo/Documents/001.jpg");
$response = $request.submit("POST");
var $responseBody = $response.getBodyAsString();

the problem here is that I always get a HTTP 400 error response.

any idea how to get this working?

Notes: the endpoint is working fine when request is executed in Postman

Answers

  • rohanrohan Members

    HTTP 400 implies bad request. Possibly, something is missing in your request. To debug this, you can open the Postman console (Ctrl+Alt+C), and observe the request headers and request body. Verify if it is added in Sahi Rest request.

    P.S You can easily import a request from Postman, using Sahi Pro v11.1.

Sign In or Register to comment.