Posting nested params with curl -F flag - php

I’m trying to post an image and a few nested parameters to an api using django rest framework. I’m trying to set up a curl with -F flags as discussed here with nested params as discussed here:
curl -X POST -S -H 'Accept: application/json' -F "customer[name]=foo&customer[email]=foo#bar.com&customer[zipcode]=1076AL&customer[city]=Amsterdam&customer[address]=foobar" -F "photo=#/Users/vincentvanleeuwen/Desktop/tmp/accord.jpg;type=image/jpg" http://localhost:8000/api/orders/
But I get the following response:
{"customer":{"city":["This field is required."],"email":["This field is required."],"zipcode":["This field is required."],"name":["This field is required."]}}
There seems to be something wrong with my nesting under the -F flag, as the nested variables work if I post it like this:
curl -X POST -S -H "Content-Type: application/json" -d '{"customer":{"name":"Adriaan","email":"adriaan#adriaan.com","zipcode":"1901ZP","address":"caravan","city":"Verweggistan"}}' http://localhost:8000/api/orders/
Any ideas what I’m doing wrong? Any help would be much appreciated!

Try separate -F flags for each parameter? From the curl manual:
Emulate a fill-in form with -F. Let's say you fill in three fields
in a form. One field is a file name which to post, one field is your
name and one field is a file description. We want to post the file
we have written named "cooltext.txt". To let curl do the posting of
this data instead of your favourite browser, you have to read the
HTML source of the form page and find the names of the input fields.
In our example, the input field names are 'file', 'yourname' and
'filedescription'.
curl -F "file=#cooltext.txt" -F "yourname=Daniel" \
-F "filedescription=Cool text file with cool text inside" \
http://www.post.com/postit.cgi

Related

How to Post data from external HTML form to Eloqua Form using cURL

I want to post data from an external HTML form to a simple form created in Eloqua, using cURL method. I tried to follow this documentation but I am not able to post data to Eloqua.
When I try from command line (Windows) -
curl --user "usercreds" --header "Content-Type: application/json" --request POST --data "{"testfirstname":"abc","testlastname":"def","singleCheckbox":1}" https://secure.p0{POD Number}.eloqua.com/api/REST/1.0/data/form/{formid}
I get below error:
[{"type":"ObjectValidationError","property":"fieldValues","requirement":{"type":"NoDuplicatesRequirement"},"value":""}]
When I try from PHP, as mentioned here https://www.eehelp.com/question/using-curl-to-repost-to-eloqua-data/
or https://github.com/fredsakr/eloqua-php-request the curl returns HTTP code 0.
This is a simple form created in Eloqua without any validations.
I do not know what I am doing wrong here.

How to submit arrays without keys via php curl?

I need to submit various fields via curl request. Some fields are simple types like strings or integers, some are files and some are arrays.
The target server is not able to accept array fields in case of submitting with keys in field name. They should be submitted with fieldnames like fieldname[] and NOT via fieldname[0].
From the docs i got this working example:
curl -H "Authorization: __TOKEN__" \
-H "Accept: application/json" \
-H "Extended-errors: true" -X POST \
-F 'foo[field_a]=Dev' \
-F 'foo[field_b]=https://example.org' \
-F 'foo[field_c][]=bar1' \
-F 'foo[field_c][]=bar2' \
-F 'foo[field_c][]=bar3' \
-F 'foo[field_d]=#/path/to/file.jpg' -g -v \
"https://api.example.com/endpoint"
I need to do this via PHP using the curl extension and tried this:
$url = 'https://api.example.com/endpoint';
$fields = [
'foo[field_a]' => 'Dev',
'foo[field_b]' => 'https://example.org',
'foo[field_c][0]' => 'bar1',
'foo[field_c][1]' => 'bar2',
'foo[field_c][2]' => 'bar3',
'foo[field_d]' => new CURLFile('/path/to/file.jpg'),
];
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
//execute post
$result = curl_exec($ch);
As you can see i had do use indexes for field_c because i cannot define foo[field_c][] twice in $fields array. In case of having just one item in array it works with key foo[field_c][]!
However, foo[field_c][0] does not work for the target server. I don't know if this is a bug or not but it is the case. If i do it this way from command line it fails, too.
If I use no indexes it works. So I need to be able to send arrays like this fieldname[] via php.
I also tried to define foo as full array and set the postfields via http_build_query($fields). But with the same result because http_build_query creates fieldnames with indexes, too.
Is there some way to send array fields without an index via php curl?
Is this remote hosts behaviour a bug? I.e. it is somewhere specified in specs that the server should accept arrays with keys?

submitting post request to curl

Im totally new in using curl or other submission services. As i'm following this link to submit an indoor map using wrld3d api they've stated the following line in order to submit a post request
$ curl -v -XPOST https://indoor-maps-api.wrld3d.com/v1/edits/?token=dev_auth_token -F name="my venue name" -F venue_street_address="<address>" -F venue_phone_number="<phone no.>" -F venue_email="<email address>" -F submission_contact_email="<email address for notifications>" -F venue_outline="#/path/to/my/file"
I tried filling in the values such as dev_auth_token with my respective account's developer token and other values like "my venue name","".. etc but i guess im going wrong as the command isn't running.Is there any syntax to follow? Here is how i filled up the command and here is the command after executing
When your URI contains some data wrap it with double quota
$ curl -v -X POST "https://indoor-maps-api.wrld3d.com/v1/edits/?token=dev_auth_token&name=my venue name&venue_street_address=<address>&venue_phone_number=<phone no.>&venue_email=<email address>&submission_contact_email=<email address for notifications>&venue_outline=#/path/to/my/file"
Same here: CURL Command Line URL Parameters. You have just trouble with parameters. Also, ... you can try with your command but using -d (that stay for data) instead of -F.
-F, --form
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an # sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between # and < is then that # makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.
Example: to send an image to a server, where 'profile' is the name of the form-field to which portrait.jpg will be the input:
curl -F profile=#portrait.jpg https://example.com/upload.cgi
To read content from stdin instead of a file, use - as the filename. This goes for both # and < constructs. Unfortunately it does not support reading the file from a named pipe or similar, as it needs the full size before the transfer starts.
You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:
curl -F "web=#index.html;type=text/html" example.com
or
curl -F "name=daniel;type=text/foo" example.com
You can also explicitly change the name field of a file upload part by setting filename=, like this:
curl -F "file=#localfile;filename=nameinpost" example.com
If filename/path contains ',' or ';', it must be quoted by double-quotes like:
curl -F "file=#\"localfile\";filename=\"nameinpost\"" example.com
or
curl -F 'file=#"localfile";filename="nameinpost"' example.com
Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.
See further examples and details in the MANUAL.
This option can be used multiple times.
This option overrides -d, --data and -I, --head and --upload.

Post json data stored in variable using curl

Sending POST request data stored in a variable using curl, sends $variable instead json data.
P=`/usr/bin/sudo /usr/bin/curl -X POST -H "Content-Type:application/json" --data-urlencode $data http://127.0.0.1/abc.php`
Trying to send POST request to php, but it receives $data instead json data{"abc":"11","xyz":"20"}.
Had try with '$data', "$data", \'$data\' and \"$data\", where $data = {"abc":"11","xyz":"20"}
Please give an example that works. Thanks in advance.
P=`/usr/bin/sudo /usr/bin/curl -X POST -H "Content-Type:application/json" -d "$O" http://127.0.0.1/abc.php` solves issue.
If you add single quote it won't expand variable, so it requires to add double quote.
I suggest all time reload page or script, as I have seen if you not reload, it work with your last change instead new one.

Smartsheet API error when using curl to update column picklist values

Good Morning,
I'm looking for a way to update the dropdown values in a column in my Smartsheet. Going off the Smartsheet API 2.0, I managed to come up with the following code to use with curl, but I'm getting the following error when running it in CMD.
Here is the code I'm using:
curl https://api.smartsheet.com/2.0/sheets/7654838910642052/columns/4 -H "Authorization: Bearer 6cn0otb4tdjueqzuflgnkzff17" -X PUT -d '{"title":"Weekend Date","index":4, "type" : "PICKLIST", "options" :["31-OCT-2015"]}' -k
The error message I get from CMD, is as follows:
C:\New>curl https://api.smartsheet.com/2.0/sheets/7654838910642052/columns/4 -H "Authorization: Bearer 5cn0otb4tdjueqzuflgnkzff17" -X PUT -d '{"title"
:"Weekend Date","index":4, "type" : "PICKLIST", "options" :["31-OCT-2015"]}' -k
{"errorCode":1124,"message":"Invalid Content-Type header. Media type not supported."}curl: (6) Could not resolve host: type; Host not found
¶hA▓╔ôÒ±$═»ù0♠~¡lk§☺▄ÜQ­K¡^ Õf ƒîa♀ÛæçÂ"õ8Ê╝±↕åÊJcurl: (6) Could not resolve host: PICKLIST,; Host not found
curl: (6) Could not resolve host: options; Host not found
curl: (3) [globbing] error: bad range specification after pos 3
Would appreciate any help I can get!!! This error is really annoying, and I have spent a good few hours trying to fix it.
** Note that I have changed the access token for security reasons, but the token I am using is definitely valid **
As Joseph suggested in his answer, you'll certainly want to verify that the value you're specifying for {columnId} in the URL is valid. However, the error you're getting is likely unrelated to that issue.
What's likely happening is that cURL is automatically setting the Content-Type header for you to the value "application/x-www-form-urlcoded" -- which is not a valid content type for this request to Smartsheet. You can resolve this error by simply setting the Content-Type header yourself in the request (i.e., add this: -H "Content-Type: application/json" to the request).
For example, my request to update the picklist options for a column such that the only option is "31-OCT-2015" looks like this:
curl https://api.smartsheet.com/2.0/sheets/{sheetId}/columns/{columnId} -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X PUT -d "{\"type\":\"PICKLIST\", \"options\":[\"31-OCT-2015\"]}" -k
Note that to update picklist options, the only attributes I need to set in the JSON are type and options. Also note that I used (escaped) double quotes in the JSON instead of single quotes -- you may or may not need to do this (depending on your platform).
It looks like the path you're using may be incorrect. I can see there's a "4" included in the path where the Column ID should be. For updating Column(s), you'd use the following path:
curl
https://api.smartsheet.com/2.0/sheets/{sheetId}/columns/{columnId}
You can find the API reference here.

Categories