Home > Enterprise >  Curl with json and file
Curl with json and file

Time:11-05

I want to send a file along with json data to my end point, something like this:

curl --location --request POST 'abc.com/upload'
--header 'Authorization: Bearer '
--header 'Content-Type: application/json'
--data-raw '{
"user": {"email":"[email protected]"},
"options": {"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"},
"import-file": "/path/to/file.csv"
}'

I want to achieve something like this, how can I do this?

CodePudding user response:

The problem may be as is, your curl is not an "application/json" request.

It arrives to "abc.com" as Content-Type: application/x-www-form-urlencoded

I ran your request to my curl test site and this is what it returned:

Content-Length: 177
Content-Type: application/x-www-form-urlencoded
Accept: */*

BODY=
{ "user": {"email":"[email protected]"}, "options": {"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"}, "import-file": "/path/to/file.csv" }
BODY urlencoded
{ "user": {"email":"[email protected]"}, "options": {"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"}, "import-file": "/path/to/file.csv" }

$_SERVER['QUERY_STRING'])


argv

$_POST
array (
  '{_"user":_{"email":"abx@xyz_com"},_"options":_{"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"},_"import-file":_"/path/to/file_csv"_}' => '',
)
$_GET
array (
)
$_REQUEST
array (
  '{_"user":_{"email":"abx@xyz_com"},_"options":_{"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"},_"import-file":_"/path/to/file_csv"_}' => '',
)
$_FILES
array (
)
$_SERVER
array (
  'CONTENT_LENGTH' => '177',
  'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
  'CONTEXT_DOCUMENT_ROOT' => '/home3/el/public_html',
  'CONTEXT_PREFIX' => '',
  'DOCUMENT_ROOT' => '/home3/el/public_html',
  'GATEWAY_INTERFACE' => 'CGI/1.1',
  'HTTP_ACCEPT' => '*/*',
  'PATH' => '/bin:/usr/bin',
  'QUERY_STRING' => '',
  'REDIRECT_STATUS' => '200',
  'REMOTE_PORT' => '44160',
  'REQUEST_METHOD' => 'POST',
  'REQUEST_SCHEME' => 'http',
  'SERVER_PORT' => '80',
  'SERVER_PROTOCOL' => 'HTTP/1.1',
  'SERVER_SIGNATURE' => '',
  'SERVER_SOFTWARE' => 'Apache',
  'TZ' => 'America/New_York',
  'UNIQUE_ID' => 'Y2YLKkUvOXKNProdJ9j6hQAAAAc',
  'REQUEST_TIME_FLOAT' => 1667631914.040588,
  'REQUEST_TIME' => 1667631914,
  'argv' => 
  array (
  ),
  'argc' => 0,
)

My question to you is can you have your "end point" retrieve the contents of /path/to/file_csv or do you have a necessity to have the client retrieve the csv and pass it to the "end point"?

CodePudding user response:

You would have to pass the <path-to-json-file> after -d option, e.g.:

curl --location --request POST 'abc.com/upload'
--header 'Authorization: Bearer '
--header 'Content-Type: application/json'
-d @<path-to-json-file>

You can find more information here.

  • Related