How to Post Url in Data of a Curl Request

How to POST URL in data of a curl request

Perhaps you don't have to include the single quotes:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/&fileName=1.doc"

Update: Reading curl's manual, you could actually separate both fields with two --data:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/" --data "fileName=1.doc"

You could also try --data-binary:

curl --request POST 'http://localhost/Service' --data-binary "path=/xyz/pqr/test/" --data-binary "fileName=1.doc"

And --data-urlencode:

curl --request POST 'http://localhost/Service' --data-urlencode "path=/xyz/pqr/test/" --data-urlencode "fileName=1.doc"

Send request to cURL with post data sourced from a file

You're looking for the --data-binary argument:

curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "@path/to/file"

In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

How to POST URL in data of a curl request

Perhaps you don't have to include the single quotes:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/&fileName=1.doc"

Update: Reading curl's manual, you could actually separate both fields with two --data:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/" --data "fileName=1.doc"

You could also try --data-binary:

curl --request POST 'http://localhost/Service' --data-binary "path=/xyz/pqr/test/" --data-binary "fileName=1.doc"

And --data-urlencode:

curl --request POST 'http://localhost/Service' --data-urlencode "path=/xyz/pqr/test/" --data-urlencode "fileName=1.doc"

How to post raw body data with curl?

curl's --data will by default send Content-Type: application/x-www-form-urlencoded in the request header. However, when using Postman's raw body mode, Postman sends Content-Type: text/plain in the request header.

So to achieve the same thing as Postman, specify -H "Content-Type: text/plain" for curl:

curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/

Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network tab :

Sample Image

How to Retrieve cURL Data within the URL Endpoint

You can access to these data via the $_SERVER superglobal.

Endpoint :

<?php

$user = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW']; // password123

PHP cURL post data is not sent to target url

I have my own solution for this.
I just need to set the URL to https insetad of http, because the backend program is redirecting all the non secure protocol http to the secure one https.

Post data to external URL using cURL

You can use this class like

class YourClass
{

private static $base_url = 'http://localhost/projects/';
private static $key = 'your_key';
private static $secret = 'your_secret';

public static function yourMethodName($id = '', $fields = '', $is_return_transfer = 0)
{

if (is_array($fields))
{
$fields_str = http_build_query($fields, '', '&');
} else
{
$fields_str = $fields;
}
$fields_str = $fields_str . "&key=" . self::$key . "&secret=" . self::$secret;
$id_str = ($id != '') ? "/$id" : "";
$url = self::$base_url . $id_str;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_str);
if ($is_return_transfer == 1)
{
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

}

You can use this like YourClass::yourMethodName($id, $fields, $is_return_transfer);



Related Topics



Leave a reply



Submit