Generate a cURL request from a Postman - http

I tried generating a cURL request from Postman (using code option of postman).
As the request contains an input pdf file, there are certain header properties that are being added by postman.
Below is the curl that is generated (almost similar, changed some header for security reasons), response received 500, internal Server Error, "Current request is not a multipart request"
http://localhost:8080/test \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded,multipart/form-data; boundary=--------------------------895926775956600620357522' \
-H 'Some-Key: abcd' \
-H 'cache-control: no-cache' \
-F file=#/C:/path/to/my/file/abc.pdf

If you want to send the multipart request in Postman, you just need to do the following:
Don't specify a Content-Type in Header.
In Body tab of Postman you should select form-data and select file type
Read more here.

Related

Why can't the exported cURLS be imported back to POSTMAN?

The latest update of the POSTMAN (7.16) doesn't seem to understand the cURL when importing them.
Here is a cURL exported by POSTMAN itself via the "Code" option.
curl -L -X POST 'https://example.com/example/' \
-H 'Content-Type: application/json' \
--data-raw '{
"requestFields":{
"address":"123, Main Street",
"ping":false
}
}'
But when this cURLis is tried to be imported to POSTMAN again, it doesn't populate the Body area of the request.
Is there a workaround?

What does -d stand for in curl request?

I am trying to send this HTTP request in Postman application:
curl -v https://api.someurl.com/z1/lists \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: authorization" \
-d '{ "list_id": "DXVBDAD" }'
Any body knows what -d stands for? and where should I put it in Postman?
The documentation says this:
(HTTP) Sends the specified data in a POST request to the HTTP server[...]
So this will be the body of your POST request. In Postman you have to put it into the 'body' field. There select 'raw' and then select 'application/json'.
Because that's the Content-Type of your request, specified with -H.
The -d or --data option makes the curl command send data in POST request to the server. This option makes the curl command pass data to the server using content-type (JSON in your case) just as the browser does when a user submits a form.

Activate KAA configuration using Server REST APIs

I used kaa rest api for activate my configuration (using Postman), but i have no idea how to fill Parameter content type of configurationId . I have try
{
"id" : "98593",
"applicationId": "32769",
"schemaId": "65544" ,
"endpointGroupId": "98308"}
but I get HTTP/1.1 400 Bad Request, any suggestion will be appreciated.
Thanks
finally i can solve this problem,
You need to set your content-type to application/json and POST body just fill with configuration id
curl -v -S -u username:password -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '12423' 'http://localhost:8080/kaaAdmin/rest/api/activateConfiguration' | python -mjson.tool

Curl-convert to a simple http request?

I have some curl request, in which i would like to build from it a basic POST request-authenticated (with headers etc), i couldn't find any tool that convert that :
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--data-urlencode 'To=xxxxxxx65542' \
--data-urlencode 'From=+xxxxxxx4215' \
-d 'Url=https://api.twilio.com/2010-04-01' \
-d 'Method=GET' \
-d 'FallbackMethod=GET' \
-d 'StatusCallbackMethod=GET' \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx
So how would my POST request should look like ?
Okay so i was trying to figure out how to send a text message from an ESP8266 nodeMcu v0.9 module. It is capable of working like an arduino with the arduino ide 1.6.4.
Anyways, I found http://textbelt.com and it only shows a simple CURL way of sending the text message.
This is the CURL message it wants you to send
$ curl -X POST http://textbelt.com/text \ -d number=5551234567 \ -d "message=I sent this message for free with textbelt.com"
So to convert to a normal HTTP POST command i did the following. (this works in Arduino IDE)
number and message are String objects.
String messageToSend = "number="+number+"&message="+message;
client.print("POST /text HTTP/1.1\r\n");
client.print("Host: textbelt.com\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.print(messageToSend.length());
client.print("\r\n\r\n");
client.print(messageToSend);
At first i tried without the Content-Type but that didn't seem to work. So i had to add the type of content i was sending as well.
If you were to monitor the network traffic with say WireShark you would see
POST /text HTTP/1.1\r\n
Host: textbelt.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 48\r\n
\r\n
number=5551234567&message=this is a text message
I may have been able to use less text with text/plain as the content-type but i think it may need the application urlencoded type to work.
Hope this helps someone else trying to convert curl to http.
From the cURL man page :
-H, --header (HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you
should add a custom header that has the same name as one of the
internal ones curl would use, your externally set header will be
used instead of the internal one. This allows you to make even
trickier stuff than curl would nor‐ mally do. You should not replace
internally set headers without knowing perfectly well what you're
doing. Remove an internal header by giving a replacement without
content on the right side of the colon, as in: -H "Host:". If you send
the custom header with no-value then its header must be terminated
with a semicolon, such as -H "X-Custom- Header;" to send
"X-Custom-Header:".
curl will make sure that each header you add/replace is sent with the
proper end-of-line marker, you should thus not add that as a part of
the header content: do not add newlines or carriage returns, they will
only mess things up for you.
See also the -A, --user-agent and -e, --referer options.
This option can be used multiple times to add/replace/remove multiple
headers.
Amazon AWS makes heavy use of headers for authentication. A quick Google should lead to many examples such as this one from http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash :
curl -X PUT -T "${file}" \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${file}
All you need to do is adapt the AWS examples for your twilio use.
To make the example into a POST, just change PUT to POST and add your POST fields -d "field1=val1&field2=val2&field3=val3"
You can specify the headers using --header and the type of request (POST) by using -X / --request parameters.
Example:
curl --request POST --header "X-MyHeader: MyAuthenticatedHeader" www.stackoverflow.com
In your case it should be:
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--header 'X-MyHeader: MyAuthenticatedHeader' \
--data-urlencode 'To=xxxxxxx65542' \
--data-urlencode 'From=+xxxxxxx4215' \
-d 'Url=https://api.twilio.com/2010-04-01' \
-d 'Method=GET' \
-d 'FallbackMethod=GET' \
-d 'StatusCallbackMethod=GET' \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx

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.
A couple of things here;
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
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']);

Resources