Search code examples
httpcurlrequesthttp-put

Is it possible to use `--data-urlencode` and `--data-binary` options for the same curl command?


I am using curl and I would like to execute a HTTP PUT request by sending a --data-urlencode string and a --data-binary JSON file content. Is it possible to make that in the same curl command?

I tried the following

curl www.website.org --request PUT -H Content-Type: application/json --data-urlencode "key=sample_string" --data-binary @sample_file.json

but it seems do not work as expected: key=sample_string and sample_file.json content are not send at all.


Solution

  • A couple of things here;

    1. Your curl request is missing double quotes for the headers. It should rather be:
    curl www.website.org --request PUT -H "Content-Type: application/json" \
     --data-urlencode "key=sample_string" --data-binary @sample_file.json
    
    1. Your content-type is application/json which I hope is not "binary", so you should rather be using an appropriate type.

    In any case, you should be able to find the submitted values using a simple php script as follows:

    $putfp = fopen('php://input', 'r');
    $putdata = '';
    while($data = fread($putfp, 1024))
        $putdata .= $data;
    fclose($putfp);
    
    var_dump($putdata);
    
    echo "---CONTENT_TYPE---\n";
    var_dump($_SERVER['CONTENT_TYPE']);