cURL POST --data-binary vs --form - post

I have a simple question regarding to the usage of cURL. Didn't find much during my Google search or Man page to get a clear answer.
In here talks about using either --data vs --form on sending file/attachment. I'm curious to know what are the main difference and under what scenarios you would pick --data-binary VS --form ?
The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):
-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=#foovalue.txt" # the specified file is sent as an attachment
-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=#foovalue.txt"
-d "#entirebody.txt" # the specified file is used as the POST body
--data-binary "#binarybody.jpg"

The difference is explained in the HTML 4.01 Specification section on Forms:
application/x-www-form-urlencoded is the default content type.
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

That's exactly the main difference, type of data that's being sent to the server (application/x-www-form-urlencoded vs multipart/form-data)

Related

Ratpack/Groovy - read the body of a call

I want to create a "mock" server using Ratpack/Groovy and we need to read the data-raw from a call
I haven't had a problem reading headers or parameters, but I need to read the body of a request
Examplle:
curl --location --POST request 'http://localhost:5050/authenticate' \
--header 'Content-type: application/json' \
--raw-data '{
"username": "testbase01#domain.com",
"password": "12345678"
}'
we need to parse and process the username and password fields
We have tried to read the request.body, or the body.text, but we have not been successful.
Basically the example found in the solution at https://stackoverflow.com/questions/23636248/parse-json-in-ratpack-groovy would be what we need, but it hasn't worked for us either
Our environment has the following dependencies
#Grab('io.ratpack:ratpack-groovy:1.6.1'),
#Grab('org.slf4j:slf4j-simple:1.7.22'),
#Grab("io.ratpack:ratpack-jackson:0.9.17")
and we are using https://github.com/Vad1mo/hello-world-rest as primary image
If you have a small example, to be able to adapt it to our needs

Create KB article via ServiceNow Table API

We're trying to create ServiceNow KB article using ServiceNow Table API
url = 'https://devXXXX.service-now.com/api/now/table/kb_knowledge'
Currently we're passing html content data (with tags) in ‘text’ field and able to create KB article
[..]
response = requests.post(url, auth=(user, password), headers=headers, data="{\"active\":\"True\",\"valid_to\":\"01-01-2100\",\"short_description\":\"KB Article\",\"text\":\"<h1> Example Article </h1> <p>Testing</p>}")
[..]
Instead of passing the html content in ‘text’ field, we're trying options to read the entire html file or any file content and create KB article in ServiceNow
Any suggestions please..
From the docs section Table - POST /now/table/{tableName} it seems like what you might be looking for is the "data" option - it allows you to put any type of text/XML into the article.
# Example 1 Python XML
response = requests.post(url,
auth=(user, pwd),
headers=headers,
data="""<request><entry>
<short_description>Unable to connect to office wifi</short_description>
<assignment_group>
287ebd7da9fe198100f92cc8d1d2154e
</assignment_group>
<urgency>2</urgency>
<impact>2</impact>
</entry></request>"""
)
# Example 2 CURL - JSON
curl "https://instance.servicenow.com/api/now/table/incident" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{'short_description':'Unable to connect to office wifi','assignment_group':'287ebd7da9fe198100f92cc8d1d2154e','urgency':'2','impact':'2'}" \
--user 'username':'password'
If you want to use HTML it seems you'll have to stick with text for that case, as it doesn't seem like they are using "text" option for anything in their Tables API (might be the case that you're using a deprecated API option for that so be careful)
Instead of posting the actual html in the data field, maybe you could just put the URL to actual page, but store the actual page elsewhere.

Twilio - How to update Task attributes defined in IVR flow for an inbound call?

I am wondering how to update event.TaskAttributes.variable value of a current task. I thought it's straight forward from the example by Twilio but I am not seeing any change If I followed the same and try to set/update a value of a specific attribute (defined in IVR voice flow widget).
https://www.twilio.com/docs/taskrouter/api/task#action-update
For example, there is an attribute called language which is a gather input digits field in IVR flow and at some point in the execution (while the caller is in waiting queue), we would like to update it to a different value. I tried via postman but it does nothing. Any help is greatly appreciated.
https://taskrouter.twilio.com/v1/Workspaces/WSXXXXXXX/Tasks/WTXXXXXX
{
"attributes": {
"language": "6"
}
}
Thanx!
In Postman use url-encoded format. The request should be encoded. Use 'Attributes' in place of 'attributes'. I am attaching a sample cURL request, this might be helpful
curl -X POST \
https://taskrouter.twilio.com/v1/Workspaces/WS...../Tasks/WTXXXX...... \
-H 'Authorization: Basic XXXXXXXXXXXXX' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'Attributes=%7B%22test2%22%3A%201%7D'

Is Avro Schema Registry compatibility lost when using arrays?

I'm using Confluent Avro Schema Registry with backward compatibility option. I upload this schema which is a record inside an array (the [ ]-brackets). This record only contains one string field.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname\", \"type\": \"string\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
Everything goes well and I get a success response back (200).
Now I want to sanity check this so I remove the string field and I add a integer field. This should definitely not be compatible, neither forward nor backwards.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname2\", \"type\": \"int\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
The problem is that this also returns a success even though this cannot possibly be correct. Am I missing something very basic here?
If I do this with a record that isn't inside an array I get the expected error from the schema registry.

mime-encoded as application/octet-stream

I am doing curl POST to a service with body containing some json data. I am getting the response as "request body was not mime-encoded as application/octet-stream". What does the response mean?
You probably need to supply a Content-Type header. Depending on what your web server is expecting, you might want to supply it either the mimetype "text/plain" or perhaps "application/x-www-form-urlencoded". In Curl, just include the argument:
-H "Content-Type: text/plain"
So your request will be something like:
curl -i -X POST --data-binary "#your.json" -H "Content-Type: text/plain"
Or substitute "text/plain" for the appropriate mime-type.
So, probably what's happening at the moment is that your web server is being given the content type application/octet-stream, and not understanding what to do with your plain text json content. If you run curl with "-v" it will give you a verbose description of the sent and received headers so you can see what Content-Type it's giving your server by default.
The input have to be a "application".
What you asking for is a mimetype.
Read more here: http://en.wikipedia.org/wiki/Internet_media_type
Please add more information for more informations.

Resources